Teleport Player to Previous Position

Okay so basically I need to be able to save vector 3 coordinates locally (I don’t Care how, just not through a Database) and then teleport the player back there. This is what I currently have (It’s not all of the script, but the rest is off topic)

local VectorToString = function(vec)
	return vec.X..' '..vec.Y..' '..vec.Z
end
wait(0.1)
local vts = VectorToString(curpos)

wait(0.1)
plr:LoadCharacter(true) --The player died, respawn them.
playerworkspace.Head.Position = Vector3.new(vts)

In there it mentions ‘playerworkspace’ This is my vaariable for the player in the workspace. It works and I’ve tested it. ‘curpos’ Is a local variable that has the Vector3 location of the head before the reset. Any help is appreciated!

2 Likes

Why can’t you just set vts to the vector itself? That is:

local vts = curpos
--…
playerworkspace.Head.Position = vts

If for some reason that doesn’t work or you need it as a string, you can use string.split to separate the string into 3 numbers.

-- what you were doing until this line
playerworkspace.Head.Position = Vector3.new(unpack(vts:split(" ")))

Edit: You don’t even need vts in the first code sample. Just do playerworkspace.Head.Position = curpos since curpos is your vector.

1 Like

If I do that then it won’t teleport to the location before the player got re spawned.

It’s easy to save Vector3 values, even locally. Just do Instance.new(“Vector3Value”), then change its value.

1 Like

Userdata values (and by extension, Vector3Values) can’t be saved in a DataStore, so OP has to serialise it by turning it into a string that can be converted back and forth from String-Vector3 and vice versa. That’s what they’re attempting to do, but their implementation is, well, wrong.

2 Likes

It still will not work even after creating that value. It will keep updating while I move.

Sorry didn’t mean to confuse, I just need to get the value once and it not update again.

Wait, I’m confused a bit. Do you want to store it in a database?

No just as a local value that won’t update

Do you need to use it in only one script or more than one?

Just one, in the one I provided

That should be easy, lets say I wanted the vector3 of the baseplate.

local BaseplatePosition = workspace.Baseplate.Position

Image example of it being done on a new baseplate:
image

I just typed a whole response but I’m a little confused by the thread’s content and archived it. What is it you’re trying to accomplish? Does your code not work, or is there another concern with your code?

I’m trying to keep a vector 3 value the same before i get respawned, so I can get teleported back to the point I was at before the respawn, then delete that vector 3 value.

Would it not be as simple as creating the value, setting it before a reset, setting the root back to that position after respawning and then just removing it?

Where is your script?

local playername = plr.Name
local playerworkspace = game.Workspace[playername]

local curpos = playerworkspace.Head.Position
local vecval = Instance.new("Vector3Value")
vecval.Parent = script
vecval.Value = curpos
warn(vecval.Value)
local chngmorph = morph:Clone()
chngmorph.Parent = game.StarterPlayer

wait(0.1)
plr:LoadCharacter(true) --The player died, respawn them.
playerworkspace.Head.Position = Vector3.new(curpos)
wait(0.1)
chngmorph:Destroy()
vecval:Destroy()

This is what I have currently.

I said where is your script, not what’s in your script.

Oh sorry, it’s in ServerScriptService

There’s a couple of strange conventions here that I don’t quite understand. I think the implementation is fundamentally flawed and needs to be redone. What’s the specific use case you’re going for?

In terms of the conventions and whatnot that I don’t understand, they’re as follows:

  • What’s the purpose in fetching a player’s character via workspace[playerName]? There’s a property under a player, Character, that references their character.

  • I see what you’re trying to do in placing a character model in StarterPlayer and I want to tell you that this is not how you use StarterCharacter.

    • If you want to change a player’s character altogether, you have to manually implement a majority of the spawning procedures yourself.
  • You don’t need to create a Vector3 value here at all if it’s only a temporary value. You just need to hold the root’s position in a variable and set it back. The variable will get cleaned once it goes out of scope.

That being said, I’ve had a look through the post some (and noticed I misunderstood part of your post in that you aren’t looking to save via DataStore) but I’m not quite sure what your source of error is. Would you mind restating that so I can get a clear sense of what the issue is?

Using the workspace is easier for me.

The community guide says to use this way of using a custom model other than using the script that changes it so I just went off that. I also have precautions for in case it fails.

When I create a variable that gets the player’s head position before the refresh, but whenever it respawns me, it updates the variable after I’m back at spawn.