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

Why does this localscript break when my character dies?

Asked by 5 years ago

Someone please tell my why this localscript breaks whenever I reset/die tyvm (Located in starterplayerscripts)

local player = game.Players.LocalPlayer
wait(3)
local previousvalue = 100
local gui = Instance.new("ScreenGui", player.PlayerGui)
local frame = Instance.new("Frame", gui)
gui.ResetOnSpawn = false
gui.Name = "Red"
frame.Size = UDim2.new(0, 10000, 0, 10000)
frame.Position = UDim2.new(0.5, -5000, 0.5, -5000)
frame.BorderSizePixel = 0
frame.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
frame.BackgroundTransparency = 1
player.Character.Humanoid.HealthChanged:Connect(function()
    if player.Character.Humanoid.Health < previousvalue then
        if player.Character.Humanoid.Health > 75 then
            frame.BackgroundTransparency = 0.75
            while frame.BackgroundTransparency < 1 and wait(0.01) do
                frame.BackgroundTransparency = frame.BackgroundTransparency + 0.01
            end
        else
            if player.Character.Humanoid.Health > 50 then
                frame.BackgroundTransparency = 0.50
                while frame.BackgroundTransparency < 1 and wait(0.01) do
                    frame.BackgroundTransparency = frame.BackgroundTransparency + 0.01
                end
            else
                if player.Character.Humanoid.Health > 25 then
                    frame.BackgroundTransparency = 0.25
                    while frame.BackgroundTransparency < 1 and wait(0.01) do
                        frame.BackgroundTransparency = frame.BackgroundTransparency + 0.01
                    end
                else
                    if player.Character.Humanoid.Health > 0 then
                        frame.BackgroundTransparency = 0
                        while frame.BackgroundTransparency < 1 and wait(0.01) do
                            frame.BackgroundTransparency = frame.BackgroundTransparency + 0.01
                        end
                    else
                        frame.BackgroundTransparency = 0
                        while frame.BackgroundTransparency < 1 and wait(0.1) do
                            frame.BackgroundTransparency = frame.BackgroundTransparency + 0.01
                        end
                    end
                end
            end
        end
    end
end)
while wait() do
    previousvalue = player.Character.Humanoid.Health
end

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
5 years ago

The problem

Your Character.Humanoid will become nil, as it gets completely re-added by roblox, therefor the event handler will be collected by the GC.

Solution

Put your event from line 13 in a CharacterAdded event and you should be fine.

Ad

Answer this question