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

Why isn't this respawn script working?

I have a script inside the StarterGui that is supposed to fire whenever a player dies. Here's the script (the script isn't a localscript):

local player = script.Parent.Parent
repeat wait() until player.Character
repeat wait() until player.Character:findFirstChild("Humanoid")
player.Character.Humanoid.Changed:connect(function()
	repeat wait() until player.Character
	if player.Character.Humanoid.Health <= 0 then
		print'dead'
		wait(4)
		player:LoadCharacter()
		repeat wait() until player.Character
		player.Character.Torso.CFrame = CFrame.new(game.Workspace.teleportPlayerHere.Position)
		player.Character.ForceField:Destroy()
	end
end)

The script works fine the first time a player dies, but it doesn't do anything the second time. There isn't anything in the output when the player dies the second time. Any help?

5 answers

Good attempt, although there's no need for a script like this to go in a GUI. Have it as a normal script in Workspace. I noticed you wanted to have a custom death, instead of the default. If you want to override the normal death with the LoadCharacter method, see this wiki post.

Now, once that is through, if you want to teleport a player on respawn, simply use CharacterAdded.

game.Players.PlayerAdded:connect(function(p)
	p.CharacterAdded:connect(function(char)
		char:MoveTo(game.Workspace.teleportPlayerHere.Position) -- MoveTo is the preferred way to move a model
		if char:findFirstChild("ForceField") then
			char.ForceField:Destroy()
		end
	end)
end)

3 comments

0

This is fine but it doesn't explain why my script works only once.

0

Well, once you respawn the character, the GUI resets itself, so the script goes back to the beginning. I'm surprised it even worked the first time

0

I forgot to mention that I set the PlayerGui's hidden property that resets the GUIs to false, so it doesn't reset when the player resets.

Roblox wiki is the solution :) http://wiki.roblox.com/index.php?title=API:Class/Player/LoadCharacter

local respawnDelay = 5 
 
game.Players.CharacterAutoLoads = false
 
game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		-- find the humanoid, and detect when it dies
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Died:connect(function()
				wait(respawnDelay)
				player:LoadCharacter()
			end)
		end
	end)
	player:LoadCharacter() -- load the character for the first time
end)

Use the HumanoidDied event! It's really great for dealing for when the player dies. Sorry about the old post; I forgot LoadCharacter dosen't work in a LocalScript. A server script would make this very quick. It'd make referencing the character a lot more easy!

game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(function(chr)
local hum = chr:FindFirstChild('Humanoid')
chr:MoveTo(workspace['teleportPlayerHere'].Position)
chr:FindFirstChild("ForceField"):Destroy()
hum.Died:connect(function()
print'dead'
wait(4)
plr:LoadCharacter()
end)
end)
end)

4 comments

0

The ":LoadCharacter()" method will not execute in a LocalScript.

0

:LoadCharacter() only works in a normal script so I can't use a LocalScript :/

0

Thanks for the catch; Edited Post.

0

I think you just made a paradox; the script will only fire if the player is added, but the player is only added inside the script since AutoPlayerLoads is turned off.

try this: game.Players.Playeradded:connect(function(p) local ondied = script.Ondied:Clone() ondied.Parent = p.Character end)' the content inside the script would be: while true do if script.Parent.Humanoid.Health == 0 then game.Players.LocalPlayer.PlayerGui.Guinamehere.Guiframenamehere.Visible = true end end` the script that is copied **HAS **to be a LocalScript

I ended up fixing the script. At the end of the code, I cloned the script and deleted the previous one. It's a rather sloppy fix, but hey, at least it works.

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