Jump to content

Problem with tabels


TwicH

Recommended Posts

Hello guys could somebody tell me where I went wrong with the tables?

I'm basically trying to give each zombie an id so they can follow the player

  
local maxzombies = 30 
local zombiecount = 0 
zombie{} 
  
  
function spawn() 
    local x,y,z = getElementPosition (localPlayer) 
    zombie[tostring(zombiecount)] = zombiecount 
    local zombie[zombiecount] = createPed (20,x+math.random(-7,7),y+math.random(-7,7),z) 
  
end 
     
function follow() 
    zpx,zpy,zpz = getElementPosition (zombie[zombiecount]) 
    local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
     
    local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
    if (dist > 2) then 
        setPedCameraRotation (zombie[zombiecount],zombie_angle) 
        setPedControlState(zombie[zombiecount], "forwards", true) 
    elseif (dist < 2) then 
        setPedControlState(zombie[zombiecount], "forwards", false) 
        setPedControlState(zombie[zombiecount], "fire", true) 
    end 
end 
setTimer (follow, 1000, 0) 
  
  
function zombie_check() 
    if (zombiecount < maxzombies) then   
        spawn() 
        zombiecount = zombiecount + 1 
    end 
end 
setTimer (zombie_check, 100, 0) 
  
function zombie_died() 
    zombiecount = zombiecount - 1 
end 
  
addEventHandler("onClientPedWasted", getRootElement(), zombie_died) 
  

Link to comment
local maxzombies = 30 
local zombiecount = 0 
zombie =  {} 
  
  
function spawn() 
    local x,y,z = getElementPosition (localPlayer) 
    zombie[tostring(zombiecount)] = zombiecount 
    local zombie[zombiecount] = createPed (20,x+math.random(-7,7),y+math.random(-7,7),z) 
  
end 
    
function follow() 
    zpx,zpy,zpz = getElementPosition (zombie[zombiecount]) 
    local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
    
    local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
    if (dist > 2) then 
        setPedCameraRotation (zombie[zombiecount],zombie_angle) 
        setPedControlState(zombie[zombiecount], "forwards", true) 
    elseif (dist < 2) then 
        setPedControlState(zombie[zombiecount], "forwards", false) 
        setPedControlState(zombie[zombiecount], "fire", true) 
    end 
end 
setTimer (follow, 1000, 0) 
  
  
function zombie_check() 
    if (zombiecount < maxzombies) then  
        spawn() 
        zombiecount = zombiecount + 1 
    end 
end 
setTimer (zombie_check, 100, 0) 
  
function zombie_died() 
    zombiecount = zombiecount - 1 
end 
  
addEventHandler("onClientPedWasted", getRootElement(), zombie_died) 

Link to comment
local maxzombies = 30 
local zombiecount = 0 
zombie =  {} 
  
  
function spawn() 
    local x,y,z = getElementPosition (localPlayer) 
    zombie[tostring(zombiecount)] = zombiecount 
    local zombie[zombiecount] = createPed (20,x+math.random(-7,7),y+math.random(-7,7),z) 
  
end 
    
function follow() 
    zpx,zpy,zpz = getElementPosition (zombie[zombiecount]) 
    local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
    
    local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
    if (dist > 2) then 
        setPedCameraRotation (zombie[zombiecount],zombie_angle) 
        setPedControlState(zombie[zombiecount], "forwards", true) 
    elseif (dist < 2) then 
        setPedControlState(zombie[zombiecount], "forwards", false) 
        setPedControlState(zombie[zombiecount], "fire", true) 
    end 
end 
setTimer (follow, 1000, 0) 
  
  
function zombie_check() 
    if (zombiecount < maxzombies) then  
        spawn() 
        zombiecount = zombiecount + 1 
    end 
end 
setTimer (zombie_check, 100, 0) 
  
function zombie_died() 
    zombiecount = zombiecount - 1 
end 
  
addEventHandler("onClientPedWasted", getRootElement(), zombie_died) 

still not working, 9: unexpected symbol near '['

Link to comment
     local maxzombies = 30 
    local zombiecount = 0 
    zombie =  {} 
      
      
    function spawn() 
        local x,y,z = getElementPosition (localPlayer) 
        zombie[zombiecount] = zombiecount 
       zombie[zombiecount] = createPed (20,x+math.random(-7,7),y+math.random(-7,7),z) 
      
    end 
        
    function follow() 
        zpx,zpy,zpz = getElementPosition (zombie[zombiecount]) 
        local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
        
        local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
        if (dist > 2) then 
            setPedCameraRotation (zombie[zombiecount],zombie_angle) 
            setPedControlState(zombie[zombiecount], "forwards", true) 
        elseif (dist < 2) then 
            setPedControlState(zombie[zombiecount], "forwards", false) 
            setPedControlState(zombie[zombiecount], "fire", true) 
        end 
    end 
    setTimer (follow, 1000, 0) 
      
      
    function zombie_check() 
        if (zombiecount < maxzombies) then 
            spawn() 
            zombiecount = zombiecount + 1 
        end 
    end 
    setTimer (zombie_check, 100, 0) 
      
    function zombie_died() 
        zombiecount = zombiecount - 1 
    end 
      
    addEventHandler("onClientPedWasted", getRootElement(), zombie_died) 

You can't local a table in this case here.

Link to comment
function follow() 
     
    for i=0,maxzombies do 
         
        local x,y,z = getElementPosition (localPlayer) 
        local zpx,zpy,zpz = getElementPosition (zombie[i]) 
  
        local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
        local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
        if (dist > 2) then 
            setPedCameraRotation (zombie[i],-zombie_angle) 
            setPedControlState(zombie[i], "backwards", true) 
        elseif (dist < 2) then 
            setPedCameraRotation (zombie[i],-zombie_angle) 
            setPedControlState(zombie[i], "fire", false) 
            setPedControlState(zombie[i], "fire", true) 
        end 
    end 
end 

I have fixed the problem but now the zombies just punch me once when they get close and never punch me again

Link to comment
function follow() 
        
        for i,v in pairs(zombie) do 
            
            local x,y,z = getElementPosition (getLocalPlayer()) 
            local zpx,zpy,zpz = getElementPosition (v) 
      
            local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
            local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
            if (dist > 2) then 
                setPedCameraRotation (v,-zombie_angle) 
                setPedControlState(v, "backwards", true) 
                setPedControlState(v, "fire", false) 
            elseif (dist < 2) then 
                 setPedAimTarget (v, x,y,z) 
                setPedCameraRotation (v,-zombie_angle) 
                setPedControlState(v, "fire", false) 
                setPedControlState(v, "fire", true) 
            end 
        end 
    end 

- Changed a few things, some due to potential issues; others just to optimize the code a bit. Any ways

make sure the timer is set up right and what not.

Link to comment
function follow() 
        
        for i,v in pairs(zombie) do 
            
            local x,y,z = getElementPosition (getLocalPlayer()) 
            local zpx,zpy,zpz = getElementPosition (v) 
      
            local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
            local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
            if (dist > 2) then 
                setPedCameraRotation (v,-zombie_angle) 
                setPedControlState(v, "backwards", true) 
                setPedControlState(v, "fire", false) 
            elseif (dist < 2) then 
                 setPedAimTarget (v, x,y,z) 
                setPedCameraRotation (v,-zombie_angle) 
                setPedControlState(v, "fire", false) 
                setPedControlState(v, "fire", true) 
            end 
        end 
    end 

- Changed a few things, some due to potential issues; others just to optimize the code a bit. Any ways

make sure the timer is set up right and what not.

now the zombies punch me once but wont punch again until i get far away from them

Link to comment

Made a mistake..

    function follow() 
            
            for i,v in pairs(zombie) do 
                
                local x,y,z = getElementPosition (getLocalPlayer()) 
                local zpx,zpy,zpz = getElementPosition (v) 
          
                local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
                local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
                if (dist < 2) then 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "backwards", true) 
                    setPedControlState(v, "fire", false) 
                elseif (dist > 2) then 
                     setPedAimTarget (v, x,y,z) 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "fire", false) 
                    setPedControlState(v, "fire", true) 
                end 
            end 
        end 

Had it so it would fire if the distance was bigger then two instead of smaller then.

Link to comment
Made a mistake..
    function follow() 
            
            for i,v in pairs(zombie) do 
                
                local x,y,z = getElementPosition (getLocalPlayer()) 
                local zpx,zpy,zpz = getElementPosition (v) 
          
                local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
                local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
                if (dist < 2) then 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "backwards", true) 
                    setPedControlState(v, "fire", false) 
                elseif (dist > 2) then 
                     setPedAimTarget (v, x,y,z) 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "fire", false) 
                    setPedControlState(v, "fire", true) 
                end 
            end 
        end 

Had it so it would fire if the distance was bigger then two instead of smaller then.

I'm sorry to bother you but it seems like every time you fix a bug there is another one coming up, now they don't even follow me unless I get close to them. would you mind we talk on skype?

Link to comment

Oh wait I read that wrong crap..

    function follow() 
            
            for i,v in pairs(zombie) do 
                
                local x,y,z = getElementPosition (getLocalPlayer()) 
                local zpx,zpy,zpz = getElementPosition (v) 
          
                local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
                local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
                if (dist > 3) then 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "backwards", true) 
                    setPedControlState(v, "fire", false) 
                elseif (dist < 3) then 
                     setPedAimTarget (v, x,y,z) 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "fire", false) 
                    setPedControlState(v, "fire", true) 
                    setPedControlState(v, "fire", false) 
                end 
            end 
        end 

There are a few bits I'm unsure of, but this should work hopefully.

Link to comment
Oh wait I read that wrong crap..
    function follow() 
            
            for i,v in pairs(zombie) do 
                
                local x,y,z = getElementPosition (getLocalPlayer()) 
                local zpx,zpy,zpz = getElementPosition (v) 
          
                local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
                local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
                if (dist > 3) then 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "backwards", true) 
                    setPedControlState(v, "fire", false) 
                elseif (dist < 3) then 
                     setPedAimTarget (v, x,y,z) 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "fire", false) 
                    setPedControlState(v, "fire", true) 
                    setPedControlState(v, "fire", false) 
                end 
            end 
        end 

There are a few bits I'm unsure of, but this should work hopefully.

now they don't punch at all

Link to comment

You want it like this? not tested.

local maxzombies = 30 
local zombiecount = 0 
zombie =  {} 
  
  
function spawn() 
    local x,y,z = getElementPosition (localPlayer) 
    zombie[zombiecount] = createPed (20,x+math.random(-7,7),y+math.random(-7,7),z) 
  
end 
    
  
   function follow() 
            
            for i,v in pairs(zombie) do 
                
                local x,y,z = getElementPosition (getLocalPlayer()) 
                local zpx,zpy,zpz = getElementPosition (v) 
                                local zombie_angle = (360 - math.deg(math.atan2((zpx - x), (zpy - y)))) % 360 
                local dist = getDistanceBetweenPoints2D(x, y, zpx, zpy) 
                if (dist > 2) then 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "backwards", true) 
                    setPedControlState(v, "fire", false) 
                elseif (dist < 1) then 
                     setPedAimTarget (v, x,y,z) 
                    setPedCameraRotation (v,-zombie_angle) 
                    setPedControlState(v, "backwards", false) 
                    setPedControlState(v, "fire", true) 
                end 
            end 
        end 
         
setTimer (follow, 1000, 0) 
  
  
function zombie_check() 
    if (zombiecount < maxzombies) then 
        spawn() 
        zombiecount = zombiecount + 1 
    end 
end 
setTimer (zombie_check, 100, 0) 
  
function zombie_died() 
    zombiecount = zombiecount - 1 
end 
  
addEventHandler("onClientPedWasted", getRootElement(), zombie_died) 
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...