Jump to content

Save player position and teleport back


spaghetti1337

Recommended Posts

I want to store the player's position and teleport them back after 2 mins.

The code above is sometimes working well sometimes just don't teleport back the player.

 

local x,y,z = getElementPosition(thePlayer) --- I define the player's position here (this is before he is warping to the vehicle)
local cloneveh = cloneElement ( vehicle, 2066.6848144531, -2496.1398925781, 13.546875 )
setElementRotation(cloneveh, 0,0, 89.148399353027)
warpPedIntoVehicle ( thePlayer, cloneveh )

setTimer (function() --- after 2 minutes I destroy the vehicle and trying to teleport back the player for his previous position
	destroyElement(cloneveh)
	setElementPosition(thePlayer, x,y,z)
end, 120000, 1)

I also tried to save the x,y,z variables with setElementData and retrieve with getElementData but I got the same result.

Link to comment

Hello spaghetti1337,

since you are not providing the entire code, for example the place where the "thePlayer" variable is being set, I can only guess. What if thePlayer is being overwritten with another player element while the timer is still ticking?

You can fix it this way:

do
    local warpPlayer = thePlayer -- the closure now has a new copy of the player element
    local x,y,z = getElementPosition(warpPlayer) --- I define the player's position here (this is before he is warping to the vehicle)
    local cloneveh = cloneElement ( vehicle, 2066.6848144531, -2496.1398925781, 13.546875 )
    setElementRotation(cloneveh, 0,0, 89.148399353027)
    warpPedIntoVehicle ( warpPlayer, cloneveh )

    setTimer (function() --- after 2 minutes I destroy the vehicle and trying to teleport back the player for his previous position
        destroyElement(cloneveh)
        setElementPosition(warpPlayer, x,y,z)
    end, 120000, 1)
end

This script does still have problems, for example it does not store the timer so that it could be stopped in case the player has left the server. You should take care of properly terminating invalid timed executions.

Edited by The_GTA
Link to comment
15 minutes ago, The_GTA said:

Hello spaghetti1337,

since you are not providing the entire code, for example the place where the "thePlayer" variable is being set, I can only guess. What if thePlayer is being overwritten with another player element while the timer is still ticking?

Hello,

What do you mean? I set the player's dimension to 0 and player's position while the timer is ticking - because before the timer is ticking I set the player's dimension to a random number. This is the full code while is the timer is ticking:
 

 

setTimer (function() --- while the timer is ticking
			destroyElement(cloneveh)
			setElementDimension(thePlayer, 0)
			setElementPosition(thePlayer, x,y,z)
			end, 120000, 1)

 

Link to comment
23 minutes ago, spaghetti1337 said:

Hello,

What do you mean? I set the player's dimension to 0 and player's position while the timer is ticking - because before the timer is ticking I set the player's dimension to a random number. This is the full code while is the timer is ticking:
 

 

setTimer (function() --- while the timer is ticking
			destroyElement(cloneveh)
			setElementDimension(thePlayer, 0)
			setElementPosition(thePlayer, x,y,z)
			end, 120000, 1)

 

I don't know what this has to do with dimensions. The code should work unless thePlayer variable is changed before the timer elapses. Therefore the part where thePlayer gets changed must be where the problem comes from, but you're not showing us that part. @The_GTA showed you how the code can be modified so that changes to thePlayer wouldn't cause this problem.

  • Like 1
Link to comment
14 minutes ago, Reyomin said:

I don't know what this has to do with dimensions. The code should work unless thePlayer variable is changed before the timer elapses. Therefore the part where thePlayer gets changed must be where the problem comes from, but you're not showing us that part. @The_GTA showed you how the code can be modified so that changes to thePlayer wouldn't cause this problem.

Exactly. I think OP is confusing code that is scheduled to run in two minutes with code that is running at the time of timer creation. And then there is a time after timer creation and before timer execution where the error may be. I am interested in that time.

49 minutes ago, spaghetti1337 said:

Hello,

What do you mean? I set the player's dimension to 0 and player's position while the timer is ticking - because before the timer is ticking I set the player's dimension to a random number. This is the full code while is the timer is ticking:
 

The code inside the timer, the function() ... end part that is the first argument to setTimer, does not appear to be the root of the problem. I think you should show us that either thePlayer is a local variable that is set somewhere above your code and modified at a later point or use the recommended solution I posted above with the local warpPlayer variable.

Edited by The_GTA
Link to comment
15 minutes ago, The_GTA said:

Exactly. I think OP is confusing code that is scheduled to run in two minutes with code that is running at the time of timer creation. And then there is a time after timer creation and before timer execution where the error may be. I am interested in that time.

The code inside the timer, the function() ... end part that is the first argument to setTimer, does not appear to be the root of the problem. I think you should show us that either thePlayer is a local variable that is set somewhere above your code and modified at a later point or use the recommended solution I posted above with the local warpPlayer variable.

Thanks both of you for help but still doing the same... it's random if the player is teleported back or no.

Well, this is a server side event so I trigger from client side - thePlayer is the localPlayer from client side.

Link to comment
2 hours ago, spaghetti1337 said:

Thanks both of you for help but still doing the same... it's random if the player is teleported back or no.

Well, this is a server side event so I trigger from client side - thePlayer is the localPlayer from client side.

Do you mind sharing your the entire serverside script so that we can take a look? Maybe there is a not-so-obvious scripting flaw that we can detect together.

Link to comment
On 25/09/2021 at 16:01, The_GTA said:

Do you mind sharing your the entire serverside script so that we can take a look? Maybe there is a not-so-obvious scripting flaw that we can detect together.

Sorry for not respoding long time ago, I was a bit busy. I think I figured out the problem. I set a timer when I set the element's position and now works fine.

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