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

Why won't my player respawn at last checkpoint touched when they rejoin server?

Hello!

I have an Obby that works great, my Leaderboard keeps track of which Stage you are on and saves the data perfectly.

However; when I rejoin my server, My character respawns at the start spawn instead of the last spawn they touched. The leaderboard will say I am currently on Stage 9, but spawns be on Stage 1.

How can I incorporate a lookup script that would teleport the player to the last touched spawn?

This is in Workspace:

function oa(object)
local player = game.Players:playerFromCharacter(object)
if player ~= nil then
local ls = player.leaderstats
local sl = game.Workspace:FindFirstChild(ls.Stage.Value)
print("gah")
object.Torso.CFrame = object.Torso.CFrame + Vector3.new(0,3,0)
wait()
object.Torso.CFrame = sl.CFrame + Vector3.new(0,3,0)
end end

function oe(object)
if object.className == "Player" then
local ack = Instance.new("IntValue")
ack.Name = "leaderstats"
local ack2 = Instance.new("IntValue")
ack2.Name = "Stage"
ack2.Value = 1
ack2.Parent = ack
ack.Parent = object
end end

game.Players.ChildAdded:Connect(oe)
game.Workspace.ChildAdded:Connect(oa)

And this is located inside of my checkpoints:

function ot(hit)
if hit.Parent ~= nil then
local player = game.Players:playerFromCharacter(hit.Parent)
if player ~= nil then
if player.leaderstats.Stage.Value == script.Parent.Name - 1 then
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.Health ~= 0 then
player.leaderstats.Stage.Value = script.Parent.Name
end end end end end end

script.Parent.Touched:Connect(ot)

local spawn = script.Parent
spawn.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = game.ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Model", game.ServerStorage)
			checkpointData.Name = "CheckpointData"
		end
		
		local checkpoint = checkpointData:FindFirstChild(tostring(player.userId))
		if not checkpoint then
			checkpoint = Instance.new("ObjectValue", checkpointData)
			checkpoint.Name = tostring(player.userId)
			
			player.CharacterAdded:Connect(function(character)
				wait()
				character:WaitForChild("HumanoidRootPart").CFrame = game.ServerStorage.CheckpointData[tostring(player.userId)].Value.CFrame + Vector3.new(0, 4, 0)
			end)
		end
		
		checkpoint.Value = spawn
	end
end)

Any help would be appreciated. I have played around with Player Lookup with Datastore but obviously am missing something.

Thanks!

3 comments

0

when you reset does it teleport you to the stage 9 spawn?

0

playerFromCharacter is deprecated, use GetPlayerFromCharacter.

0

Yes, When I reset the teleportation works fine. When I quit the game and rejoin I am placed back at the beginning spawn but my level still says I am on Stage 9

1 answer

✓ Accepted answer

This is what i found : The torso is removed from the game its replaced with HumanoidRootPart

function oa(object)
local player = game.Players:playerFromCharacter(object)
if player ~= nil then
local ls = player.leaderstats
local sl = game.Workspace:FindFirstChild(ls.Stage.Value)
object.HumanoidRootPart.CFrame = object.HumanoidRootPart.CFrame + Vector3.new(0,3,0)
wait()
object.HumanoidRootPart.CFrame = sl.CFrame + Vector3.new(0,3,0)
end end

function oe(object)
if object.className == "Player" then
local ack = Instance.new("IntValue")
ack.Name = "leaderstats"
local ack2 = Instance.new("IntValue")
ack2.Name = "Stage"
ack2.Value = 1
ack2.Parent = ack
ack.Parent = object
end end

game.Players.ChildAdded:Connect(oe)
game.Workspace.ChildAdded:Connect(oa)

if still doesn't work tell me

3 comments

0

Still isn't spawning players at last touched checkpoint when they rejoin server. It says I am on "Stage 10" but starts me back at Stage 1 upon Rejoin. Playing the game works great and respawns you back at last touched checkpoint when you die or reset. But I need this to somehow communicate with the leaderboard Datastore and teleport the player to last touched checkpoint upon Re-Joining server

0

There is a better way. You could add an auto saving leaderstats script. And make the checkpoints named as the number of that current stage, and then another script that when a character is added in the workspace (either when a player respawned or joined) it will get its stage value and find the part in the workspace or wherever you put your checkpoints that is named (or numbered) the same as

0

the value of the stage, then it will get the cframe of it and move the characters humanoidrootpart cframe to it, and add a vector3 of 0,12,0 so it doesn't spawn inside the spawnpoint

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