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

Roblox save and load player's position not working properly?

local KeyData = require(script.Parent.DataSettings)

local DSS = game:GetService("DataStoreService")
local plrPosition = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-plrPosition")
--above part isn't really important
game.Players.PlayerAdded:Connect(function(player)
	local plrdata = player:WaitForChild("PlayerData")
	local pos = Instance.new("StringValue")
	player.CharacterAdded:Connect(function(character)
		wait()
		repeat
			pos.Name = "PosValue"
			pos.Parent = plrdata
			pos.Value = plrPosition:GetAsync(player.UserId) or "0, 3, 0"
			local coord = {}
			for num in string.gmatch(pos.Value, "[-%d%.]+") do
				table.insert(coord, tonumber(num))
			end
	        character.HumanoidRootPart.Position = Vector3.new(unpack(coord))
		until character.HumanoidRootPart.Position == Vector3.new(unpack(coord))
        game.Players.PlayerRemoving:Connect(function(player)
            plrPosition:SetAsync(player.UserId, tostring(pos.Value))
        end)
		while wait(15) do
			pos.Value = tostring(character.HumanoidRootPart.Position)
			plrPosition:SetAsync(player.UserId, tostring(pos.Value))
		end
	end)
end)

Basically, when a player joins create a new string value. Whilst their character is alive, save it's HumanoidRootPart's position every 15 seconds. the repeat loop makes sure the player loads at their position . local coord variable and stuff below is converting the string into a Vector3 value. Whilst a new player successfully teleports to a fixed, set position, for some reason when a coming player joins there is a chance the script does not load the player's position and instead just spawns them at 0,Y,0, the default position roblox spawns you at. Can someone tell me what the problem is with not always loading the player's position? I do not want to use CFrame. There are also no errors in output.

1 answer

✓ Accepted answer

There's actually such thing as a Vector3Value which would be a lot more convenient compared to using a StringValue. You should also be disconnecting the PlayerRemoving connection when the player leaves so it's no longer referenced. When the player leaves as well, the while loop continues on and will never stop. Be sure to break the loop when they leave. When you set the player's position, it probably won't exactly be that position so the repeat loop may never end which starts the saving process. I also don't see a point in the repeat loop since when you set the position, it will set the position? I recommend putting prints around your code to see where it's running and where it's stopping for debugging..

8 comments

0

I've heard they can't be stored in datastores, tried myself and couldn't store them aswell.

0

Yea you can't save them, but it would be much easier to just convert it into a table rather then a string. {Vector.X, Vector.Y, Vector.Z}

0

I'll consider that, but I still don't know what's causing the problem of the script not loading the player's position half the time

0

Like I said, did you check if it's even saving, try printing?

0

I put a print statement after the CharacterAdded function and weirdly enough it's not printing anything depite my character fully loaded.

0

And to answer your question, sometimes it's saving and other it's not.

0

I would recommend AncestryChanged over PlayerRemoving as well so you can see when the actual player leaves the game rather then getting notified for each one. Your WaitForChild could be yielding or it could be yielding long enough to where the character has already loaded in thus not firing the event.

0

Thanks for your help though, turns out I previously was using Vector3Values incorrectly. I'll mark your answer as your solution.

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