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

How do i wait until a player has respawned?

I want to teleport a player once they have respawned or change their respawn point. i was thinking something like this maybe would work but it didn't:

local checkfolder = game.Workspace.checkpoints
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Checkpoint = Instance.new("IntValue",leaderstats)
	Checkpoint.Name = "Stage"
	
	for i,v in pairs(checkfolder:GetChildren()) do
		if v:IsA("BasePart") then
			v.Touched:Connect(function(hit)
				local char = hit.Parent
                local plr = Players:GetPlayerFromCharacter(char)
                if i == 1 then
                    Checkpoint.Value = 2
					if plr then
						plr.CharacterAdded:Wait()
						char:WaitForChild("HumanoidRootPart").CFrame = v.CFrame * CFrame.new(0, 3, 0)
					end
				end
				if i == 2 then
					Checkpoint.Value = 3
				end		
				if i ==3 then
					Checkpoint.Value = 4
				end
				if i == 4 then
					Checkpoint.Value = 5
				end
				if i == 5 then
					Checkpoint.Value = 6
				end
				if i == 6 then
					Checkpoint.Value = 7
				end
				if i == 7 then
					Checkpoint.Value = 8
				end
				if i == 8 then
					Checkpoint.Value = 9
				end
			end)
		end
	end
end)

it didn't work, so i set respawn time to 0 and removed the code

repeat wait() until humanoid.Health == 100

but it still didn't work

Edit: I posted the full script so you can find out what the problem is. The output isn't processing any errors.

Checkpoint.Value will equal 1 when it steps on the spawn and that is another script. This is the other script:

game.Players.PlayerAdded:Connect(function(plr)
	local Checkpoint = plr.leaderstats.Stage
	for i,v in pairs(script.Parent:GetChildren()) do
		if v:IsA("SpawnLocation") then
			v.Touched:Connect(function(char)
				if char.Parent:FindFirstChild("Humanoid") then
					Checkpoint.Value = 1
				end
			end)
		end
	end
end)

So i am unable to wait when character is added otherwise the checkpoint value will equal to 0

9 comments

0

There is a event of Player call CharacterAdded, the event will fire when the player spawn or respawn https://developer.roblox.com/api-reference/event/Player/CharacterAdded

0

You can wait the player respawn by simply type this: plr.CharacterAdded:Wait()            * :Wait() is different from Wait(), we use :Wait() to make the script wait until a event fire.

0

I've already tried this, but it doesn't work

0

the "plr" is either the left leg or the right leg

0

Wait does it throw any error?

0

no

0

And what happen when the script run?

0

The leaderstats works except the checkpoint value stays 0 when i join. Also when i die, I spawn at the start. I'm sorry for bothering you, i'm just not experienced and i'm only 13 years old

0

I think this post will help you make a checkpoint system https://education.roblox.com/en-us/resources/intro-to-studio-checkpoints and I'm 13 too :D

3 answers

✓ Accepted answer

#Problems

  1. On your first and second script you Connect to the Touched event eacht time a player Joins. but if multiple joins and someone steps on it everyones leaderstat get changed.

  2. You are trying to change a Players respawn point

#Solutions

  1. Instead of Connecting to the Touched even each time a player joins. Connect to it once and if the Touched parent has a Humanoid use Players:GetPlayerFromCharacter to get the Player

  2. You can change a Players respawn point by changing its Player.RespawnLocation property.

#Final Scripts Script 1 put in ServerScriptService

local checkfolder = game.Workspace.checkpoints
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local Checkpoint = Instance.new("IntValue",leaderstats)
    Checkpoint.Name = "Stage"
end)

Script 2 put in the Checkpoints folder

for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("SpawnLocation") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChildOfClass("Humanoid") then
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- Gets the player from the character
				local Checkpoint = plr.leaderstats.Stage
				
				Checkpoint.Value = i + 1

				plr.RespawnLocation = v
			end
		end)
	end
end

12 comments

0

ok i'll try

0

It doesn't change the respawn location of the player

0

hmm

0

make sure the Spawnlocations Neutral property is set to true

0

ok

0

It is but it still doesn't work. It comes up with this error in the output:

0

Expected SpawnLocation got Part for Player:RespawnLocation.

0

make sure those are spawnlocations

0

edited second script

0

ah k, I'll try that! Btw thanks for your support, i really appreciate it

0

accept this answer if it Worked

0

It respawns me in the correct place however fsr, the value of stage becomes a random number, any ideas why?

You cant detect if the health if a died humanoid goes up after it has respawned. if a player respawns the player fires the function MakeJoints and on died BreakJoints. If a player has fired the function BreakJoints, the humanoid is indeed still there until the player has officially respawned, when the player respawns they get a new humanoid which has to be called in the script. but that's impossible since the script is waiting for the old (deleted) humanoid for its health to go back go 100 which isn't happening.

What you either can do is identify if a player has indeed respawned and then fire the function you want to.

3 comments

0

How do i do that?

0

This is extremely inaccurate, AFAIK. 'MakeJoints' and 'BreakJoints' are methods of models (not humanoids). (Ex: Model:MakeJoints(), Model:BreakJoints()). Read more about it here: https://developer.roblox.com/en-us/api-reference/function/Model/MakeJoints and here: https://developer.roblox.com/en-us/api-reference/function/Model/BreakJoints

0

Really, this isn't very precise, but you could simply use a wait() syntax and play your game, and once you die, count how many seconds it takes for you to re spawn. Then, put the number of seconds in the wait().

Oh right I forgot, you need to get the "real" player by this:

local Players = game:GetService("Players") -- we need Players service to get the player from character

v.Touched:Connect(function(hit) --you should change the parameter name to another name because maybe the thing that touch the part maybe is not a player
	local char = hit.Parent
	local plr = Players:GetPlayerFromCharacter(char) -- this function will get the player instance from character model
	if plr then
		plr.CharacterAdded:Wait() --this will make the script wait until the event fire, in this case wait until the player respawn

		char:WaitForChild("HumanoidRootPart").CFrame = v.CFrame * CFrame.new(0,3,0)
	end
end)

Hope this helping you.

2 comments

0

It still doesn't work :(

0

Ill post the full script

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