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

why the event isn't happening again ?

plr = game.Players.LocalPlayer
chr = game:GetService("Workspace"):WaitForChild(plr.Name)
human = chr:WaitForChild("Humanoid")
ui = plr.PlayerGui:WaitForChild("base")
rs = game:GetService("ReplicatedStorage")


human.HealthChanged:Connect(function()
ui.health.value.Text = human.Health
  if human.Health <= 0 then
	game:GetService("Lighting").Blur.Enabled = true
	rs.health:FireServer()
  end
end)

plr.CharacterAdded:Connect(function()
	game:GetService("Lighting").Blur.Enabled = false
end)

this script works only one time....then when the player's character is added it doesn't update the ui.health.Value.Text (it stays 0) and the screen isn't blured if he dies again...i figured out that the human.HealthChanged isn't happening anymore but i don't know why

(a local script inside StarterPlayerScripts)

(sorry i am not a native english speaker)

4 comments

0

When a player respawns and character is created again, it creates a new Humanoid. The human.HealthChanged function uses the old Humanoid and so it won't fire for the new Humanoid.

0

that's true..but i don't know how to fix it

0

you should probably use startercharacterscripts, just a thought. starterplayerscripts only give you scripts when the player joins. when the player dies and respawns, the humanoid variable you made will point to the old humanoid which won't work in your favor. tl;dr: use StarterCharacterScripts, StarterPack, or StarterGui. Or move your script into the CharacterAdded function

0

i know but i thought there is way to fix it by just changing the variables...but puting the script into StarterCharacterScripts seems to be the only way........thank you ^^

1 answer

✓ Accepted answer

The problem When a player respawns and character is created again, it creates a new Humanoid. The human.HealthChanged function uses the old Humanoid and so it won't fire for the new Humanoid.

Option 1 Use StarterCharacterScripts in order to have the LocalScript be remade every time the character dies.

Option 2 Use Player.CharacterAdded

plr.CharacterAdded:Connect(function(chr)
	local human = chr:WaitForChild("Humanoid")
	human.HealthChanged:Connect(function()
		ui.health.value.Text = human.Health
		if human.Health <= 0 then
			game:GetService("Lighting").Blur.Enabled = true
			rs.health:FireServer()
		end
	end)
end)

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