Script deactivates when death but reactivates when player has respawn

This is my 2nd post. How do I make a script that deactivates a certain script when player died but the script reactivates again when the player respawned? Help is appreciated!

1 Like

I am confused on what you mean, when a player’s character resets, all local scripts should be resetting themselves.

Do you mean this for server scripts?

If it’s a local script, put it in starter character scripts. That way, it will reset every time the player respawns.

3 Likes
local certain_script;

local function enableScript(enable)
    certain_script.Enabled = enable;
end

game.Players.PlayerAdded:Connect(function(player)
    local death_listener;
    player.CharacterAdded:Connect(function(character)
        death_listener = character:WaitForChild('Humanoid').Died:Connect(function()
            enableScript(false);
        end)
        enableScript(true):
    end)
end)

It is actually not necessary for a local script to be in StarterPlayerScripts to reset on a death, it should still reset in PlayerGui when the character resets.

Edit: @JoelBrd use the event PlayerAdded not PlayerJoin, I don’t even know if that was a thing in lua before, but if it was then it is depreciated.

Thanks for spotting that mistake @Hello42bacon!

So I have a overhead damage gui. Whenever I died there is a -1 thingy over my head. So I want to disable the damage overhead gui script when I died and and reactivates the script when I respawn.

Are you using a local script, it should be resetting if so and you should be using one for a health GUI. If that still doesn’t work then there is a problem with your script. Try printing something in the output to see if it resets, that way we know if it is your actual code or not.

It’s a script not a local script.

local bill = Instance.new(“BillboardGui”)
bill.Parent = script.Parent
bill.Active = false
bill.Adornee = script.Parent.Head
bill.AlwaysOnTop = false
bill.Enabled = true
bill.Size = UDim2.new(1, 0, 1, 0)
bill.StudsOffset = Vector3.new(-0.5, 5, 0)
local currentHealth = script.Parent.Humanoid.Health
script.Parent.Humanoid.HealthChanged:connect(function(newHealth)
local healthChange = math.abs(currentHealth - newHealth)
sym = (currentHealth > newHealth and “-” or “+”)
if sym == “+” then
if healthChange >= 5 then
text = Instance.new(“TextLabel”, bill)
text.BackgroundTransparency = 1
text.Name = “ObtainHealth”
text.Size = UDim2.new(2, 0, 2, 0)
text.Font = “SourceSansBold”
text.FontSize = “Size48”
text.Text = “+” … math.floor(healthChange)
text.TextColor3 = Color3.new(85 / 255, 1, 0)
text.TextScaled = true
text.TextStrokeColor3 = Color3.new(50 / 255, 150 / 255, 73 / 255)
text.TextStrokeTransparency = 0
text.TextWrapped = true
scr = script.Dissapear:clone()
scr.Parent = text
scr.Disabled = false
end
else
text = Instance.new(“TextLabel”, bill)
text.BackgroundTransparency = 1
text.Name = “TakeDamage”
text.Size = UDim2.new(2, 0, 2, 0)
text.Font = “SourceSansBold”
text.FontSize = “Size48”
text.Text = “-” … math.floor(healthChange)
text.TextColor3 = Color3.new(1, 0, 0)
text.TextScaled = true
text.TextStrokeColor3 = Color3.new(115 / 255, 0, 0)
text.TextStrokeTransparency = 0
text.TextWrapped = true
scr = script.Dissapear:clone()
scr.Parent = text
scr.Disabled = false
end
currentHealth = newHealth
end)

Here is the Overhead damage gui script. I want it to be disabled when I die and reactivate by itself when I respawn.

Next time, please use a code block.

What do you mean by it becomes -1? Im pretty sure that on death, the player’s health becomes 0, not -1.

After that, can’t you just check if the player’s health is over 0, and if it is, show the damage gui?

no no no. You don’t get it do you? So whenever I get damaged there will be a floating gui on my head. Example: If I take damage there will be a gui on top of my head. Let’s say I took 30 damage. There will be a gui with a -30 text. But what I meant is whenever I die the gui floating on my head appears and randomly floating on my head with the text -1. So I want to make a script where the Overhead damage gui script is disabled when I die so there will be no random floating -1 text over my head. But this script should be reactivate when I respawn so that if I take damage again the text is going too appear on my head and when I die again the text will not appear because the script has been disabled.

use :

function:Disconnect()

and then later you can reactivate it with

function:Connect()