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

why the event isn't happening again ?

Asked by
hmurban 16
4 years ago
Edited 4 years ago
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)

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. Overscores 381 — 4y
0
that's true..but i don't know how to fix it hmurban 16 — 4y
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 Fifkee 2017 — 4y
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 ^^ hmurban 16 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
Ad

Answer this question