Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't this respawn script working?

Asked by 10 years ago

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):

01local player = script.Parent.Parent
02repeat wait() until player.Character
03repeat wait() until player.Character:findFirstChild("Humanoid")
04player.Character.Humanoid.Changed:connect(function()
05    repeat wait() until player.Character
06    if player.Character.Humanoid.Health <= 0 then
07        print'dead'
08        wait(4)
09        player:LoadCharacter()
10        repeat wait() until player.Character
11        player.Character.Torso.CFrame = CFrame.new(game.Workspace.teleportPlayerHere.Position)
12        player.Character.ForceField:Destroy()
13    end
14end)

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

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

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.

1game.Players.PlayerAdded:connect(function(p)
2    p.CharacterAdded:connect(function(char)
3        char:MoveTo(game.Workspace.teleportPlayerHere.Position) -- MoveTo is the preferred way to move a model
4        if char:findFirstChild("ForceField") then
5            char.ForceField:Destroy()
6        end
7    end)
8end)
0
This is fine but it doesn't explain why my script works only once. whyOmustOitObeOme 7 — 10y
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 Shawnyg 4330 — 10y
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. whyOmustOitObeOme 7 — 10y
Ad
Log in to vote
1
Answered by 10 years ago

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

01local respawnDelay = 5
02 
03game.Players.CharacterAutoLoads = false
04 
05game.Players.PlayerAdded:connect(function(player)
06    player.CharacterAdded:connect(function(character)
07        -- find the humanoid, and detect when it dies
08        local humanoid = character:FindFirstChild("Humanoid")
09        if humanoid then
10            humanoid.Died:connect(function()
11                wait(respawnDelay)
12                player:LoadCharacter()
13            end)
14        end
15    end)
16    player:LoadCharacter() -- load the character for the first time
17end)
Log in to vote
0
Answered by 10 years ago

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!

01game.Players.PlayerAdded:connect(function(plr)
02plr.CharacterAdded:connect(function(chr)
03local hum = chr:FindFirstChild('Humanoid')
04chr:MoveTo(workspace['teleportPlayerHere'].Position)
05chr:FindFirstChild("ForceField"):Destroy()
06hum.Died:connect(function()
07print'dead'
08wait(4)
09plr:LoadCharacter()
10end)
11end)
12end)
0
The ":LoadCharacter()" method will not execute in a LocalScript. Redbullusa 1580 — 10y
0
:LoadCharacter() only works in a normal script so I can't use a LocalScript :/ whyOmustOitObeOme 7 — 10y
0
Thanks for the catch; Edited Post. DigitalVeer 1473 — 10y
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. whyOmustOitObeOme 7 — 10y
Log in to vote
0
Answered by 10 years ago

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

Log in to vote
-2
Answered by 10 years ago

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.

Answer this question