CharacterAdded isnt working in startergui localscript?

I have a localscript in startergui that basically checks to see if the players health is at a certain point, then change the lighting based off their situation. However, it doesnt go back to normal when the character is added back in?

local player = game.Players.LocalPlayer
local lighting = game:GetService("Lighting")

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")
local heartbeat = script.Heartbeat
local death = script.Death

player.CharacterAdded:Connect(function()
	lighting.ColorCorrection.Contrast = 0
	lighting.ColorCorrection.Saturation = 0
	lighting.ColorCorrection.Brightness = 0
	heartbeat:Stop()
end)

hum.HealthChanged:Connect(function()
	if hum.Health < 30 then
		lighting.ColorCorrection.Contrast = .2
		lighting.ColorCorrection.Saturation = -1
		lighting.ColorCorrection.Brightness = 0
		heartbeat:Play()
	end
	
	if hum.Health > 30 then
		lighting.ColorCorrection.Contrast = 0
		lighting.ColorCorrection.Saturation = 0
		lighting.ColorCorrection.Brightness = 0
		heartbeat:Stop()
	end
	
	if hum.Health <= 0 then
		lighting.ColorCorrection.Contrast = 1
		lighting.ColorCorrection.Saturation = -1
		lighting.ColorCorrection.Brightness = -.3
		heartbeat:Stop()
		death:Play()
	end
end)

I tested it out and it seems fine. Can you describe your issue in more detail?

Also, these lines conflict with each other:

if hum.Health < 30 then
	lighting.ColorCorrection.Contrast = .2
	lighting.ColorCorrection.Saturation = -1
	lighting.ColorCorrection.Brightness = 0
	heartbeat:Play()
end
if hum.Health <= 0 then
	lighting.ColorCorrection.Contrast = 1
	lighting.ColorCorrection.Saturation = -1
	lighting.ColorCorrection.Brightness = -.3
	heartbeat:Stop()
	death:Play()
end

I recommend using elseif and else to prevent this:

if hum.Health <= 0 then
    lighting.ColorCorrection.Contrast = 1
    lighting.ColorCorrection.Saturation = -1
    lighting.ColorCorrection.Brightness = -.3
    heartbeat:Stop()
    death:Play()
elseif hum.Health < 30 then
    lighting.ColorCorrection.Contrast = .2
    lighting.ColorCorrection.Saturation = -1
    lighting.ColorCorrection.Brightness = 0
    heartbeat:Play()
else
    lighting.ColorCorrection.Contrast = 0
    lighting.ColorCorrection.Saturation = 0
    lighting.ColorCorrection.Brightness = 0
    heartbeat:Stop()
end

Ah, I’m sorry, I see your issue now. The CharacterAdded event is not firing, and the effects aren’t resetting.

If your LocalScript is inside of a ScreenGui, turn off ResetOnSpawn in the properties:
image

If your LocalScript is inside of StarterGui, move the code inside CharacterAdded outside:

local player = game.Players.LocalPlayer
local lighting = game:GetService("Lighting")

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")
local heartbeat = script.Heartbeat
local death = script.Death

hum.HealthChanged:Connect(function()
	if hum.Health <= 0 then
		lighting.ColorCorrection.Contrast = 1
		lighting.ColorCorrection.Saturation = -1
		lighting.ColorCorrection.Brightness = -.3
		heartbeat:Stop()
		death:Play()
	elseif hum.Health < 30 then
		lighting.ColorCorrection.Contrast = .2
		lighting.ColorCorrection.Saturation = -1
		lighting.ColorCorrection.Brightness = 0
		heartbeat:Play()
	else
		lighting.ColorCorrection.Contrast = 0
		lighting.ColorCorrection.Saturation = 0
		lighting.ColorCorrection.Brightness = 0
		heartbeat:Stop()
	end
end)

lighting.ColorCorrection.Contrast = 0
lighting.ColorCorrection.Saturation = 0
lighting.ColorCorrection.Brightness = 0
heartbeat:Stop()

Let me know if that doesn’t fix it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.