Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Respawning where I left last time?

My title explains everything I want to do. However I have no idea how to do it. I thought of a spawnlocation but that would be very messy. So I guess the only way is datasave, how do to it correctly though? Please help!

P.S. I am not asking for a script! I just want someone to show me how I should do to accomplish my objective.

1 comment

0

Save the location when they leave in a datastore, then load it when they join and teleport them to that point.

1 answer

✓ Accepted answer

To make this, we need to save the position in a DataStore. Here is a rough example.

local PositionStore = game:GetService("DataStoreService"):GetDataStore("PositionStore")
game:GetService("Players").PlayerAdded:Connect(function(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	local Success, LastPosition = pcall(function()
		return PositionStore:GetAsync(player.UserId)
	end)
	if Success and LastPosition ~= nil then
		wait(1)
		Character:MoveTo(Vector3.new(LastPosition.X, LastPosition.Y, LastPosition.Z)
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local Character = player.Character
	pcall(function()
		local CurrentPosition = {
			X = Character.HumanoidRootPart.Position.X,
			Y = Character.HumanoidRootPart.Position.Y,
			Z = Character.HumanoidRootPart.Position.Z
		}
		PositionStore:SetAsync(player.UserId, CurrentPosition)
	end)
end)

14 comments

0

Yes, I've tried doing something like that by creating a my own code and it didn't work because of some errors. I have just tried yours and it gives no errors, but everytime I join my game I keep spawning in the place I usually spawn. (Yes I moved before leaving...) What's wrong? Could you help me further?

0

Fixer1987 Most data does NOT save in studio try it in an actual game

0

@EnzoTDZ_YT I did that! Doesn't work...

0

This code should work. I don't see any problem with it. Are you sure its a server(regular) script and not a local script? I would put this in ServerScriptService in a regular script.

0

@Alphexus It isn't. I put it in ServerScriptService as a script and it doesn't work. Everytime I leave the game I published I keep respawning...

0

Try waiting like 3 seconds before moving the character.

0

Still not working. :/

0

I see why its not working. I did not know that you cannot save Vectors in a DataStore. I will get back to you with an answer.

0

Post is edited and updated. Try out the new code. I saved the components of the position in a table and then loaded it. This should work. Please accept the answer if it works. Thanks!

0

wait wait wait... ScriptingHelpers is a site were you Help with already existing script. Not give the asker a script. and your Reputation as above 300...

0

So? Some people learn better with code examples. I chose to answer it this way even though the person clearly stated they did not want a script. I just find it easier to give a code example and a very brief explanation.

0

Tahnk you so much! Trying it right now. Will let you know.

0

Did it work?

0

you literally just gave away a script to someone who didnt even start making one

This discussion is preserved as it was. New answers, comments, and votes are closed.