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

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

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 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.

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)
0
This is fine but it doesn't explain why my script works only once. whyOmustOitObeOme 7 — 9y
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 — 9y
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 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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)
Log in to vote
0
Answered by 9 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!

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)
0
The ":LoadCharacter()" method will not execute in a LocalScript. Redbullusa 1580 — 9y
0
:LoadCharacter() only works in a normal script so I can't use a LocalScript :/ whyOmustOitObeOme 7 — 9y
0
Thanks for the catch; Edited Post. DigitalVeer 1473 — 9y
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 — 9y
Log in to vote
0
Answered by 9 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 9 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