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

How can I have a script activate ONLY during respawn?

Asked by
Zerio920 285 Moderation Voter
9 years ago

I have a server script that uses this:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)

It activates whenever a player respawns after dieing. That's fine. However in my place there's a section where the player can reload his character using

:LoadCharacter()

And the script above still activates. I only want the script to activate after the player respawns by DIEING, not by reloading his character.

2 answers

Log in to vote
2
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

What you can do is keep track of whether or not the character died between characters added. To do this, I suggest using the event Died of the object Humanoid.

game.Players.PlayerAdded:connect(function(player)

    local died = false
    local firstEntry = true

    player.CharacterAdded:connect(function(character)

        character:WaitForChild("Humanoid").Died:connect(function()
            died = true -- If player died, change variable
        end)

        if firstEntry then -- If you want to do something on entry
            print("Welcome to " .. game.Name .. ", " .. player.Name .. "!")
            firstEntry = false
        else
            if died and not firstEntry then -- If the player died then
                print(player.Name .. " has been resurrected!")
                died = false
            else -- If the player didn't die then
                print(player.Name .. " has been refreshed!")
            end
        end
    end)
end)

0
It was working a while ago, but now for some reason it's detecting a "died" as a "reload". Might just be a roblox glitch but any ideas on how to fix this? Zerio920 285 — 9y
0
Sorry, that was my bad. I was making it do the Died check on the first character that entered the game instead of the current one. It works now though! BlackJPI 2658 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
game.Players.PlayerAdded:connect(function(player)
    game.Player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            --Code
        end)
    end)
end)

The Died event happens whenever the character dies so it makes it so instead of every time they respawn it's only when they die. If you wish to make it so you also wait until they respawn and not right as they die then before the code (but still inside the Died event) you should put

repeat wait() until player.Character

Answer this question