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

My position script does not save position and does not print?

Hi, I'm making an obby and I'm trying to make a script that saves the position when a player leaves and puts them in the same position when they rejoin. It just keeps on bringing me back to the original SpawnLocation. Please help!

Code:

DataStoreService = game:GetService("DataStoreService")
positionStore = DataStoreService:GetDataStore("positionStore")

game.Players.ChildAdded:Connect(function(plr)
	local success, position = pcall(function()
		return positionStore:GetAsync(plr)
	end)
	if not position then
		local success2, err2 = pcall(function()
			positionStore:SetAsync(plr, tostring("-2, 1.5, 54"))
		end)
		if success2 then
			print("Successfully saved position for " .. plr.Name .. "!")
		elseif err2 then
			error("Error saving position for " .. plr.Name .. ". Error: " ..  err2)
		end
	else
		local character = plr.CharacterAdded:Wait()
		character:MoveTo(Vector3.new(tonumber(position)))
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success3, err3 = pcall(function()
		positionStore:SetAsync(plr, tostring(plr.Character.Position))
	end)
	if success3 then
		print("Successfully saved data for " .. plr.Name .. "!")
	elseif err3 then
		error("Error saving data for " .. plr.Name .. ". Error: " ..  err3)
	end
end)

1 comment

0

Im not sure if this would help your case, but if you could substitiute the SpawnLocation with a normal Part and just use the PlayerAdded function, and when the player touches the part (checkpoint), then you would save the players' position. Hope this helps!

1 answer

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