Jump to content

I got Error in this script


Recommended Posts

In this script no errors...whatever the ped don't even move forward and follows me.

server:

    function follow (thePlayer) 
        local x, y, z = getElementPosition(thePlayer)    
        local guard = createPed (240, x, y, z) 
        local tx, ty, tz = getElementPosition(guard)             
        local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
        setPedRotation(guard, findRotation(tx, ty, x, y))        
        if dis > 2 then 
        setElementData (guard, "Move!", guard) 
        triggerClientEvent (thePlayer, "moveGuard", guard) 
        else 
        setElementData (guard, "Stop!", guard) 
        triggerClientEvent (thePlayer, "stopGuard", guard) 
            end 
         end 
        addCommandHandler ("guard", follow) 
        function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end      
         

client:

    function moveGuard() 
    getElementData (source, "Move!", guard) 
    setPedControlState (source, "forwards", true) 
    end 
    addEvent("moveGuard",true) 
    addEventHandler ("moveGuard", getRootElement(), moveGuard)  
    function stopGuard () 
    getElementData (source, "Stop!", guard) 
    setPedControlState (source, "forwards", false) 
    end 
    addEvent("stopGuard", true) 
    addEventHandler ("stopGuard", getRootElement(), stopGuard) 

Link to comment
  • 3 months later...

Actually, source should be the ped since he's triggering the event with the ped as the sourceElement.

As for the code, it's totally messed up. You have the findRotation server-side which should be client-side. You need to use onClientRender, findRotation and set the control state.

Link to comment
Actually, source should be the ped since he's triggering the event with the ped as the sourceElement.

As for the code, it's totally messed up. You have the findRotation server-side which should be client-side. You need to use onClientRender, findRotation and set the control state.

could you help me to do this?

Link to comment

Not tested.

Server:

function follow (thePlayer) 
    local x, y, z = getElementPosition(thePlayer)    
    local guard = createPed (240, x + 2, y, z + 1) 
    triggerClientEvent(thePlayer, 'moveGuard', guard) 
 end 
addCommandHandler ("guard", follow) 

Client:

local guard 
  
function moveGuard() 
    guard = source 
    addEventHandler('onClientPreRender', root, followPlayer) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    local x, y, z = getElementPosition(localPlayer) 
    local tx, ty, tz = getElementPosition(guard)           
    local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
    setPedRotation(guard, findRotation(tx, ty, x, y))       
    if (dis > 2) then 
        setPedControlState(guard, 'forwards', true) 
    else 
        setPedControlState(guard, 'forwards', false) 
    end 
end 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end 

Link to comment
Not tested.

Server:

function follow (thePlayer) 
    local x, y, z = getElementPosition(thePlayer)    
    local guard = createPed (240, x + 2, y, z + 1) 
    triggerClientEvent(thePlayer, 'moveGuard', guard) 
 end 
addCommandHandler ("guard", follow) 

Client:

local guard 
  
function moveGuard() 
    guard = source 
    addEventHandler('onClientPreRender', root, followPlayer) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    local x, y, z = getElementPosition(localPlayer) 
    local tx, ty, tz = getElementPosition(guard)           
    local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
    setPedRotation(guard, findRotation(tx, ty, x, y))       
    if (dis > 2) then 
        setPedControlState(guard, 'forwards', true) 
    else 
        setPedControlState(guard, 'forwards', false) 
    end 
end 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end 

Actually worked Nice! :) You're Good! Thanks, but there's problem here, when i enter Interior or i be already in interior and do /guard the ped dissapear :/, could you help me with that please?

Link to comment

Client:

local guard 
  
function moveGuard() 
    guard = source 
    addEventHandler('onClientPreRender', root, followPlayer) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    local int, dim = getElementInterior(localPlayer), getElementDimension(localPlayer) 
    local tint, tdim = getElementInterior(guard), getElementDimension(guard) 
    if (int ~= tint) then setElementInterior(guard, int) end 
    if (dim ~= tdim) then setElementDimension(guard, dim) end 
    local x, y, z = getElementPosition(localPlayer) 
    local tx, ty, tz = getElementPosition(guard)           
    local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
    setPedRotation(guard, findRotation(tx, ty, x, y))       
    if (dis > 2) then 
        setPedControlState(guard, 'forwards', true) 
    else 
        setPedControlState(guard, 'forwards', false) 
    end 
end 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end     

Server is the same.

Link to comment
Client:
local guard 
  
function moveGuard() 
    guard = source 
    addEventHandler('onClientPreRender', root, followPlayer) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    local int, dim = getElementInterior(localPlayer), getElementDimension(localPlayer) 
    local tint, tdim = getElementInterior(guard), getElementDimension(guard) 
    if (int ~= tint) then setElementInterior(guard, int) end 
    if (dim ~= tdim) then setElementDimension(guard, dim) end 
    local x, y, z = getElementPosition(localPlayer) 
    local tx, ty, tz = getElementPosition(guard)           
    local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
    setPedRotation(guard, findRotation(tx, ty, x, y))       
    if (dis > 2) then 
        setPedControlState(guard, 'forwards', true) 
    else 
        setPedControlState(guard, 'forwards', false) 
    end 
end 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end     

Server is the same.

Error: Client: Line 5

addEventHandler 'onClientPreRender' with this function is already handled 

Link to comment
Client:
local guard 
  
function moveGuard() 
    guard = source 
    addEventHandler('onClientPreRender', root, followPlayer) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    local int, dim = getElementInterior(localPlayer), getElementDimension(localPlayer) 
    local tint, tdim = getElementInterior(guard), getElementDimension(guard) 
    if (int ~= tint) then setElementInterior(guard, int) end 
    if (dim ~= tdim) then setElementDimension(guard, dim) end 
    local x, y, z = getElementPosition(localPlayer) 
    local tx, ty, tz = getElementPosition(guard)           
    local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
    setPedRotation(guard, findRotation(tx, ty, x, y))       
    if (dis > 2) then 
        setPedControlState(guard, 'forwards', true) 
    else 
        setPedControlState(guard, 'forwards', false) 
    end 
end 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end     

Server is the same.

Error: Client: Line 5

addEventHandler 'onClientPreRender' with this function is already handled 

This Problem happen when i spawn more than one guard, and only one guard follows me.

Link to comment

Try this, client:

local guards = {} 
  
function moveGuard() 
    table.insert(guards, source) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    for index, guard in pairs(guards) do 
        if (not isElement(guard) or isPedDead(guard)) then table.remove(guards, index) 
        else 
            local int, dim = getElementInterior(localPlayer), getElementDimension(localPlayer) 
            local tint, tdim = getElementInterior(guard), getElementDimension(guard) 
            if (int ~= tint) then setElementInterior(guard, int) end 
            if (dim ~= tdim) then setElementDimension(guard, dim) end 
            local x, y, z = getElementPosition(localPlayer) 
            local tx, ty, tz = getElementPosition(guard)           
            local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
            setPedRotation(guard, findRotation(tx, ty, x, y))       
            if (dis > 2) then 
                setPedControlState(guard, 'forwards', true) 
            else 
                setPedControlState(guard, 'forwards', false) 
            end 
        end 
    end 
end 
addEventHandler('onClientPreRender', root, followPlayer) 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end     

Link to comment
Try this, client:
local guards = {} 
  
function moveGuard() 
    table.insert(guards, source) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    for index, guard in pairs(guards) do 
        if (not isElement(guard) or isPedDead(guard)) then table.remove(guards, index) 
        else 
            local int, dim = getElementInterior(localPlayer), getElementDimension(localPlayer) 
            local tint, tdim = getElementInterior(guard), getElementDimension(guard) 
            if (int ~= tint) then setElementInterior(guard, int) end 
            if (dim ~= tdim) then setElementDimension(guard, dim) end 
            local x, y, z = getElementPosition(localPlayer) 
            local tx, ty, tz = getElementPosition(guard)           
            local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
            setPedRotation(guard, findRotation(tx, ty, x, y))       
            if (dis > 2) then 
                setPedControlState(guard, 'forwards', true) 
            else 
                setPedControlState(guard, 'forwards', false) 
            end 
        end 
    end 
end 
addEventHandler('onClientPreRender', root, followPlayer) 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end     

Aha :D Worked man but it's possible to make them hit zombies near them by weapons or hands? and protect you if someone just shot you? please help me with that :)! ! !

and makes them jump if they're stucked and run fast if they're away from you .. I'm sorry for this long things but i'm really need help with them and , THANKS A Lot man :)

Link to comment
Try this, client:
local guards = {} 
  
function moveGuard() 
    table.insert(guards, source) 
end 
addEvent("moveGuard",true) 
addEventHandler ("moveGuard", getRootElement(), moveGuard) 
  
function followPlayer() 
    for index, guard in pairs(guards) do 
        if (not isElement(guard) or isPedDead(guard)) then table.remove(guards, index) 
        else 
            local int, dim = getElementInterior(localPlayer), getElementDimension(localPlayer) 
            local tint, tdim = getElementInterior(guard), getElementDimension(guard) 
            if (int ~= tint) then setElementInterior(guard, int) end 
            if (dim ~= tdim) then setElementDimension(guard, dim) end 
            local x, y, z = getElementPosition(localPlayer) 
            local tx, ty, tz = getElementPosition(guard)           
            local dis = getDistanceBetweenPoints2D (x, y, tx, ty) 
            setPedRotation(guard, findRotation(tx, ty, x, y))       
            if (dis > 2) then 
                setPedControlState(guard, 'forwards', true) 
            else 
                setPedControlState(guard, 'forwards', false) 
            end 
        end 
    end 
end 
addEventHandler('onClientPreRender', root, followPlayer) 
  
function findRotation(x1,y1,x2,y2) 
    local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
    if t < 0 then t = t + 360 end 
    return t 
end     

Aha :D Worked man but it's possible to make them hit zombies near them by weapons or hands? and protect you if someone just shot you? please help me with that :)! ! !

and makes them jump if they're stucked and run fast if they're away from you .. I'm sorry for this long things but i'm really need help with them and , THANKS A Lot man :)

So man :/?

Link to comment
Use element data or tables. setElementData(thePlayer, 'guard', guard)

I'm sorry but still don't understand it's like this?:

I've made this for example and want to know something:

    function follow (thePlayer) 
        local x, y, z = getElementPosition(thePlayer)   
        local guard = createPed (240, x + 2, y, z + 1) 
        triggerClientEvent(thePlayer, 'moveGuard', guard) 
        setElementData (thePlayer,"guard", guard) 
     end 
    addCommandHandler ("guard", follow) 
  
function Example () 
    if not getElementData (source, "guard", guard) then 
    outputChatBox ("You have no Guard") 
    else 
    giveWeapon (guard, 30, 200) 
    end 
    addCommandHandler ("ArmGuard", Example 

so the gun will be added to all guards with the players and i need every guard with people that have own things, not all guards have the same things when player write /ArmGuard :/...please can you help me with that?

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