Jump to content

Client Side scripts not working!


Kamran3478

Recommended Posts

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 :/)

Link to comment

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 by Prox69
Link to comment
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?

Link to comment
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

 

Link to comment
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 :(

Link to comment
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()

Link to comment
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 by Kamran3478
Link to comment
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 by Burak5312
  • Like 1
  • Thanks 1
Link to comment
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

  • Like 1
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...