85485 Posted October 9, 2022 Posted October 9, 2022 I want the ped to follow the nearest player and I use this script: function setPedFollow(theElement, theTarget) if theElement then local x, y, z = getElementPosition(theTarget) local rx, ry, rz = getElementRotation(theTarget) local ex, ey, ez = getElementPosition(theElement) local distance = getDistanceBetweenPoints3D(x, y, z, ex, ey, ez) local rotation = findRotation(ex, ey, x, y) setPedRotation(theElement, rotation) if distance > 2 then setPedControlState(theElement, "forwards", true) else setPedControlState(theElement, "forwards", false) end end end addCommandHandler("followplayer", setPedFollow) My script file type is client side and nothing happens when I use the followplayer command, I thought it was glitched, I changed the type of my script file from meta.xml to server side and tried again, I got some errors. I get that script from https://wiki.multitheftauto.com/wiki/SetPedFollow. What is wrong with my code? (btw im new to lua scripting :/)
Prox69 Posted October 9, 2022 Posted October 9, 2022 (edited) what about the theTarget have you pass it type in the console debugscript 3 to see the errors here is another exmaple have a look local ped = createPed(0, 0, 0, 5) function TestFollow() setPedFollow(ped, localPlayer) end addEventHandler("onClientRender", root, TestFollow) Edited October 9, 2022 by Prox69
85485 Posted October 10, 2022 Author Posted October 10, 2022 20 hours ago, Prox69 said: what about the theTarget have you pass it type in the console debugscript 3 to see the errors here is another exmaple have a look local ped = createPed(0, 0, 0, 5) function TestFollow() setPedFollow(ped, localPlayer) end addEventHandler("onClientRender", root, TestFollow) How that works? I meant its addEventHandler but i want to run this script with command. Can i use addCommandHandler?
Hydra Posted October 10, 2022 Posted October 10, 2022 it works only with timer or onClientRender. Also you need to put findRotation function too Social Media: Discord: Gabriel45#6859 Instagram: https://www.instagram.com/_gabriel_455/ YouTube: Hydra45 (https://www.youtube.com/c/Hydra45/)
85485 Posted October 11, 2022 Author Posted October 11, 2022 On 10/10/2022 at 17:30, Hydra said: it works only with timer or onClientRender. Also you need to put findRotation function too what is timer? can you show a example for this?
Hydra Posted October 11, 2022 Posted October 11, 2022 7 minutes ago, Kamran3478 said: what is timer? can you show a example for this? local myPed = createPed(skinID, posX, posY, posZ) setTimer(function() setPedFollow(myPed, localPlayer) end, 1000, 0) function setPedFollow(theElement, theTarget) if theElement then local x, y, z = getElementPosition(theTarget) local rx, ry, rz = getElementRotation(theTarget) local ex, ey, ez = getElementPosition(theElement) local distance = getDistanceBetweenPoints3D(x, y, z, ex, ey, ez) local rotation = findRotation(ex, ey, x, y) setPedRotation(theElement, rotation) if distance > 2 then setPedControlState(theElement, "forwards", true) else setPedControlState(theElement, "forwards", false) end end end function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end Social Media: Discord: Gabriel45#6859 Instagram: https://www.instagram.com/_gabriel_455/ YouTube: Hydra45 (https://www.youtube.com/c/Hydra45/)
85485 Posted October 12, 2022 Author Posted October 12, 2022 On 11/10/2022 at 19:57, Hydra said: local myPed = createPed(skinID, posX, posY, posZ) setTimer(function() setPedFollow(myPed, localPlayer) end, 1000, 0) function setPedFollow(theElement, theTarget) if theElement then local x, y, z = getElementPosition(theTarget) local rx, ry, rz = getElementRotation(theTarget) local ex, ey, ez = getElementPosition(theElement) local distance = getDistanceBetweenPoints3D(x, y, z, ex, ey, ez) local rotation = findRotation(ex, ey, x, y) setPedRotation(theElement, rotation) if distance > 2 then setPedControlState(theElement, "forwards", true) else setPedControlState(theElement, "forwards", false) end end end function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end I got these errors: attempt to perform arithmetic on local 'x2' (a boolean value) [DUP x4] Bad argument @ 'getElementPosition' [Expected element at argument 1, got nil] Bad argument @ 'getElementRotation' [Expected element at argument 1, got nil] Bad argument @ 'getDistanceBetweenPoints3D' [Expected vector3 at argument 1, got boolean] setTimer(function(localPlayer) setPedFollow(myPed, localPlayer) end, 1000, 0) function setPedFollow(theElement, theTarget) if theElement then local x, y, z = getElementPosition(theTarget) local rx, ry, rz = getElementRotation(theTarget) local ex, ey, ez = getElementPosition(theElement) local distance = getDistanceBetweenPoints3D(x, y, z, ex, ey, ez) local rotation = findRotation(ex, ey, x, y) setPedRotation(theElement, rotation) if distance > 2 then setPedControlState(theElement, "forwards", true) else setPedControlState(theElement, "forwards", false) end end end function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end Thats the script. Why its not working
Hydra Posted October 12, 2022 Posted October 12, 2022 23 minutes ago, Kamran3478 said: I got these errors: attempt to perform arithmetic on local 'x2' (a boolean value) [DUP x4] Bad argument @ 'getElementPosition' [Expected element at argument 1, got nil] Bad argument @ 'getElementRotation' [Expected element at argument 1, got nil] Bad argument @ 'getDistanceBetweenPoints3D' [Expected vector3 at argument 1, got boolean] setTimer(function(localPlayer) setPedFollow(myPed, localPlayer) end, 1000, 0) function setPedFollow(theElement, theTarget) if theElement then local x, y, z = getElementPosition(theTarget) local rx, ry, rz = getElementRotation(theTarget) local ex, ey, ez = getElementPosition(theElement) local distance = getDistanceBetweenPoints3D(x, y, z, ex, ey, ez) local rotation = findRotation(ex, ey, x, y) setPedRotation(theElement, rotation) if distance > 2 then setPedControlState(theElement, "forwards", true) else setPedControlState(theElement, "forwards", false) end end end function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end Thats the script. Why its not working why did you put setTimer(function(localPlayer) lmao when I put it only setTimer(function() Social Media: Discord: Gabriel45#6859 Instagram: https://www.instagram.com/_gabriel_455/ YouTube: Hydra45 (https://www.youtube.com/c/Hydra45/)
85485 Posted October 12, 2022 Author Posted October 12, 2022 (edited) 27 minutes ago, Hydra said: why did you put setTimer(function(localPlayer) lmao when I put it only setTimer(function() But thats also not working when i set setTimer(function(). And i got same errors EDIT: also, i set my script file's type to shared Edited October 12, 2022 by Kamran3478
βurak Posted October 13, 2022 Posted October 13, 2022 (edited) local x,y,z = getElementPosition(localPlayer) local myPed = createPed(0, x, y+1, z) setTimer(function() setPedFollow(myPed, localPlayer) end, 1000, 0) function setPedFollow(theElement, theTarget) if theElement then local x, y, z = getElementPosition(theTarget) local rx, ry, rz = getElementRotation(theTarget) local ex, ey, ez = getElementPosition(theElement) local distance = getDistanceBetweenPoints3D(x, y, z, ex, ey, ez) local rotation = findRotation(ex, ey, x, y) setPedRotation(theElement, rotation) if distance > 2 then setPedControlState(theElement, "forwards", true) else setPedControlState(theElement, "forwards", false) end end end function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end try this also change script file type to client if you don't have any other code in the file it works, i tested it Edited October 13, 2022 by Burak5312 1 1
85485 Posted October 13, 2022 Author Posted October 13, 2022 6 hours ago, Burak5312 said: local x,y,z = getElementPosition(localPlayer) local myPed = createPed(0, x, y+1, z) setTimer(function() setPedFollow(myPed, localPlayer) end, 1000, 0) function setPedFollow(theElement, theTarget) if theElement then local x, y, z = getElementPosition(theTarget) local rx, ry, rz = getElementRotation(theTarget) local ex, ey, ez = getElementPosition(theElement) local distance = getDistanceBetweenPoints3D(x, y, z, ex, ey, ez) local rotation = findRotation(ex, ey, x, y) setPedRotation(theElement, rotation) if distance > 2 then setPedControlState(theElement, "forwards", true) else setPedControlState(theElement, "forwards", false) end end end function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end try this also change script file type to client if you don't have any other code in the file it works, i tested it thank you so muchh. it worked 1
βurak Posted October 13, 2022 Posted October 13, 2022 (edited) if you have any questions please open a new thread and tag me, i will help you. Edited October 13, 2022 by Burak5312 1
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