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?
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)
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)
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.