Jump to content

[help] Assign NPC to a specific player


monday

Recommended Posts

Hi, my aim is to:

-assign a ped to a specific player (so I could use a command like "/pedRemoveFromPlayer Antonio_Banderas")

-be able to destroy/manipulate any ped from a bunch of them as if they had an ID

I don't know if there's a simple way to do it so any advice would be appreciated. I tried to do it my own way by creating a ped with a name made of the Player's name + a number but it doesn't work. Here's the code I used:

function AddPed(_thePlayer) 
    local x, y, z = getElementPosition(_thePlayer); 
    x = x + math.sin(math.rad(getElementRotation(_thePlayer))) * 2 
    y = y - math.cos(math.rad(getElementRotation(_thePlayer))) * 2  
    --thePed = createPed (math.random(10, 270), x, y, z) 
    pedID = pedID + 1 
    string.format(getPlayerName(_thePlayer) .. pedID) = createPed (math.random(10, 270), x, y, z) --That's the line where the error occurs 
end 
addCommandHandler ("ped", AddPed)  

The error I received is: ERROR: Loading script failed: server.lua:68: unexpected symbol near '=' (68th line is the 7th line of the code posted above)

Thanks in advance

Link to comment

I do not understand what you wanted to do, but I corrected.

  
function AddPed(_thePlayer) 
    local x, y, z = getElementPosition(_thePlayer); 
    x = x + math.sin(math.rad(getElementRotation(_thePlayer))) * 2 
    y = y - math.cos(math.rad(getElementRotation(_thePlayer))) * 2 
    --thePed = createPed (math.random(10, 270), x, y, z) 
    pedID = pedID + 1 
    string.format(getPlayerName(_thePlayer) .." "..pedID)  
createPed (math.random(10, 270), x, y, z)  
end 
addCommandHandler ("ped", AddPed)  
  
  

Link to comment
function AddPed(_thePlayer) 
    local x, y, z = getElementPosition(_thePlayer); 
    x = x + math.sin(math.rad(getElementRotation(_thePlayer))) * 2 
    y = y - math.cos(math.rad(getElementRotation(_thePlayer))) * 2 
    --thePed = createPed (math.random(10, 270), x, y, z) 
    ped = createPed (math.random(10, 270), x, y, z) 
    local pedsCount = tonumber(getElementData(_thePlayer,'peds')) or 0 
    setElementData(_thePlayer,'peds',pedsCount+1) 
    setElementData(ped,tostring(getPlayerAccount(_thePlayer)),tonumber(getElementData(_thePlayer,'peds'))) 
end 
addCommandHandler ("ped", AddPed)  
  
function RemovePed(_thePlayer,cmd,player,pedid) 
local player = getPlayerFromName(tostring(player)) 
pedsCount = tonumber(getElementData(player,'peds')) or 0 
    if pedsCount > 0 then 
        for k,v in ipairs(getElementsByType( "ped" )) do 
            if getElementData(v,tostring(getPlayerAccount(player))) and getElementData(v,tostring(getPlayerAccount(player))) == tonumber(pedid) then 
            destroyElement(v) 
            setElementData(player,'peds',pedsCount-1) 
            end 
        end 
    end 
end 
addCommandHandler("pedRemoveFromPlayer", RemovePed)  

I'm not sure if it works

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...