Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this localscript break when my character dies?

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

✓ Accepted answer

##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.

This discussion is preserved as it was. New answers, comments, and votes are closed.