Jump to content

How to kill a ped based on his ID


Recommended Posts

So, I made a script where you can create a ped and give him an ID, a model and a dimension.

function CriarPed (thePlayer, command, id, model, dimensao)
    local sX, sY, sZ = getElementPosition(thePlayer)
    ped = createPed(tonumber(model), sX, sY, sZ)
    pedID = setElementID (ped, id)
    dimensaoP = setElementDimension(ped, dimensao)
    if pedID and dimensaoP then
    qDimensao = getElementDimension(ped)
    nomePed = getElementID (ped)
    outputChatBox (horaJ.. "Criaste um ped chamado " ..corPed.. nomePed.. corMJ.. " com o modelo " ..corPed.. model.. corMJ.." na dimensão " ..corEspaco.. qDimensao.. corMJ.. ".", thePlayer, isR, isG, isB, true)
    addEventHandler("onPedWasted", ped, pedMorreu)
    addCommandHandler("matarped", matarPed)
    else
        destroyElement(ped)
        outputChatBox(horaE.. "Uso: /criarped [" ..corPed.. "Nome do Ped" ..corErro.. "] [" ..corPed.. "0" ..corErro.. "-" ..corPed.. "312" ..corErro.. "] [" ..corEspaco.. "0" ..corErro.. "-" ..corEspaco.. "65535" ..corErro.. "].", source, isR, isG, isB, true)
    end
end
addCommandHandler("criarped", CriarPed)
addCommandHandler("cp", CriarPed)
addCommandHandler("criarped", CriarPed)

function matarPed(player, command)
    if killPed (ped) then
        outputChatBox("Mataste")
    else
        outputChatBox("Erro")
    end
end

Now, what I'm trying to do, but can't, is to be able to use /matarped [pedID] to kill the ped with that ID. For now, I can only kill the last one I created.

I've read something about loops, but I'm not sure about how it works and can't apply it to my code by myself.

 

Thanks for the help! Sorry for making so many posts, but I only do them when I really can't continue all by myself, and if you notice, my coding has gotten better by each post I made and you guys help me solve. Thanks!

Link to comment
1 minute ago, Peti said:

Sorry, misunderstood your question.

I use command /cp NameOfThePed ModelOfThePed DimensionOfThePed

Ex: /cp Hugo 23 0

 

Now, I have a command to kill the ped, called /matarped. But it only kills the last ped I've created.

I want it to kill the ped that has the name that I've putten on argument1. In this case it would be /matarped Hugo

But I can't do it.

Link to comment
36 minutes ago, Peti said:

I've edited my post. Read it again, hope it helps ^_^

It doesn't work. I had already tried it like that.

It returns me this error when I type /matarped hugo:

Bad Argument @ killPed expected element at argument 1, got hugo

And this when I just type /matarped

Bad Argument @ killPed expected element at argument 1, got nill

Link to comment

Try this:

function CriarPed (thePlayer, command, id, model, dimensao)
	local sX, sY, sZ = getElementPosition(thePlayer)
	local ped = createPed(tonumber(model), sX, sY, sZ)
	local pedID = setElementID (ped, id)
	local dimensaoP = setElementDimension(ped, dimensao)
	if pedID and dimensaoP then
		local qDimensao = getElementDimension(ped)
		local nomePed = getElementID (ped)
		outputChatBox (horaJ.. "Criaste um ped chamado " ..corPed.. nomePed.. corMJ.. " com o modelo " ..corPed.. model.. corMJ.." na dimensão " ..corEspaco.. qDimensao.. corMJ.. ".", thePlayer, isR, isG, isB, true)
		addEventHandler("onPedWasted", ped, pedMorreu)
	else
		if isElement(ped) then destroyElement(ped) end
		outputChatBox(horaE.. "Uso: /criarped [" ..corPed.. "Nome do Ped" ..corErro.. "] [" ..corPed.. "0" ..corErro.. "-" ..corPed.. "312" ..corErro.. "] [" ..corEspaco.. "0" ..corErro.. "-" ..corEspaco.. "65535" ..corErro.. "].", source, isR, isG, isB, true)
	end
end
addCommandHandler("criarped", CriarPed)
addCommandHandler("cp", CriarPed)

function matarPed(player, command, pedID)
	if pedID and getElementByID(pedID) then
		killPed( getElementByID(pedID) )
		outputChatBox("Mataste")
	elseif not (getElementByID(pedID)) then
		outputChatBox("No ped with this id")
	else
		outputChatBox("Erro")
	end
end
addCommandHandler("matarped", matarPed)

 

Edited by DNL291
  • Thanks 1
Link to comment
26 minutes ago, Hugo_Almeidowski said:

It doesn't work. I had already tried it like that.

It returns me this error when I type /matarped hugo:

Bad Argument @ killPed expected element at argument 1, got hugo

And this when I just type /matarped

Bad Argument @ killPed expected element at argument 1, got nill

 

My bad. If you take a look at the function, it says:

bool killPed ( ped thePed, [ ped theKiller = nil, int weapon=255, int bodyPart=255, bool stealth = false ] )

You must provide an actual ped, not an ID, otherwise it would says "int thePed". Try getting the ped by its id, then proceed to delete it, just as DNL told you.

 

P.S.: Don't worry about making tons of posts, this is the biggest scripting forum after all, but a good practise is to look at the Wiki and the examples it gives to you.

Edited by Peti
Link to comment
1 hour ago, DNL291 said:

Try this:


function CriarPed (thePlayer, command, id, model, dimensao)
	local sX, sY, sZ = getElementPosition(thePlayer)
	local ped = createPed(tonumber(model), sX, sY, sZ)
	local pedID = setElementID (ped, id)
	local dimensaoP = setElementDimension(ped, dimensao)
	if pedID and dimensaoP then
		local qDimensao = getElementDimension(ped)
		local nomePed = getElementID (ped)
		outputChatBox (horaJ.. "Criaste um ped chamado " ..corPed.. nomePed.. corMJ.. " com o modelo " ..corPed.. model.. corMJ.." na dimensão " ..corEspaco.. qDimensao.. corMJ.. ".", thePlayer, isR, isG, isB, true)
		addEventHandler("onPedWasted", ped, pedMorreu)
	else
		if isElement(ped) then destroyElement(ped) end
		outputChatBox(horaE.. "Uso: /criarped [" ..corPed.. "Nome do Ped" ..corErro.. "] [" ..corPed.. "0" ..corErro.. "-" ..corPed.. "312" ..corErro.. "] [" ..corEspaco.. "0" ..corErro.. "-" ..corEspaco.. "65535" ..corErro.. "].", source, isR, isG, isB, true)
	end
end
addCommandHandler("criarped", CriarPed)
addCommandHandler("cp", CriarPed)

function matarPed(player, command, pedID)
	if pedID and getElementByID(pedID) then
		killPed( getElementByID(pedID) )
		outputChatBox("Mataste")
	elseif not (getElementByID(pedID)) then
		outputChatBox("No ped with this id")
	else
		outputChatBox("Erro")
	end
end
addCommandHandler("matarped", matarPed)

 

OHHHHHH Thanks man it worked! I didn't know that function existed. If I'm not mistaken, I've actually looked for something like that but couldn't find nothing. Thanks!!

1 hour ago, Peti said:

 

My bad. If you take a look at the function, it says:


bool killPed ( ped thePed, [ ped theKiller = nil, int weapon=255, int bodyPart=255, bool stealth = false ] )

You must provide an actual ped, not an ID, otherwise it would says "int thePed". Try getting the ped by its id, then proceed to delete it, just as DNL told you.

 

P.S.: Don't worry about making tons of posts, this is the biggest scripting forum after all, but a good practise is to look at the Wiki and the examples it gives to you.

Yes. Sometimes I have about 20 wiki pages opened hahaha.

Thanks!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...