TwicH Posted June 20, 2016 Posted June 20, 2016 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)
Dimos7 Posted June 20, 2016 Posted June 20, 2016 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)
TwicH Posted June 20, 2016 Author Posted June 20, 2016 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 '['
Captain Cody Posted June 20, 2016 Posted June 20, 2016 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.
TwicH Posted June 21, 2016 Author Posted June 21, 2016 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
Captain Cody Posted June 21, 2016 Posted June 21, 2016 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.
TwicH Posted June 24, 2016 Author Posted June 24, 2016 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
Captain Cody Posted June 25, 2016 Posted June 25, 2016 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.
TwicH Posted June 25, 2016 Author Posted June 25, 2016 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?
Captain Cody Posted June 25, 2016 Posted June 25, 2016 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.
TwicH Posted June 25, 2016 Author Posted June 25, 2016 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
فاّرس Posted June 26, 2016 Posted June 26, 2016 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now