under ? like this
end)
end
under ? like this
end)
end
and the lines should be down there?
Like this:
local Health = script.Parent.Health
local player = game.Players.LocalPlayer
player.CharacterAdded:Connect(function(Char)
local Humanoid = Char:WaitForChild("Humanoid")
local HCConn, DConn
HCConn = Humanoid.HealthChanged:Connect(function(CurrentHealth)
Health.Text = tostring(math.floor(CurrentHealth))
end)
Health.Text = tostring(math.floor(Humanoid.MaxHealth))
DConn = Humanoid.Died:Connect(function()
HCConn:Disconnect()
DConn:Disconnect()
Health.Text = "0"
end)
end)
EDIT
Try it now lol.
EDIT2
Ohhhhhhhhhh I didnât see you had ResetOnSpawn
disabled. Rewrote the code
now its just 0 and it never went back to 100
You want to make the script in a loop because when the character dies the character and humanoid variables wont be up to date.
its still 0 are you sure everythings correct?
Should be now lol, I didnât notice you had ResetOnSpawn
disabled. Rewrote the above code to include that.
EDIT
Nope, exactly how itâs written.
shouldnt itLocalPlayer before doing CharacterAdded?
oh wait nope nvm i see it now hmmm
That script wont work because it needs to be a server script.
This should work:
local Health = script.Parent.Health
local player = game.Players.LocalPlayer
while task.wait() do
local Char = player.Character or plr.CharacterAdded:wait()
local Hum = Char:WaitForChild("Humanoid")
Health.Text = ""..math.floor(Hum.Health)
end
well his script still works for some reason StormDragon23
Thatâs incorrect â the code you wrote will not work. LocalPlayer
is client-sided only and is nil
on the server. Regardless, this does work on a LocalScript, including the version of the program I presented.
ok it wont work cuz the humanoid will be removed once the character dies and will give a error ofc
you can either use a protected call inside the HealthChanged event or make a function will be called when the character added after the death
local Health = script.Parent.Health
local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local function OnCharacterAdded(Character)
local Humanoid = Character:WaitForChild("Humanoid")
Health.Text = tostring(math.floor(Humanoid.Health)) -- setting it for the first time
local HCConn = Humanoid.HealthChanged:Connect(function(CurrentHealth)
Health.Text = tostring(math.floor(Humanoid.Health))
end)
Humanoid.Died:Wait()
HCConn:Disconnect()
end
OnCharacterAdded(Char) -- calling it for the first time
player.CharacterAdded:Connect(OnCharacterAdded)
so which solution do i use CookieInks or StormDragon23 one?
Youâre correct and on point, but Iâll expand upon this â the problem with the original program was that it was still holding a reference to the original Humanoid
, even after the player had respawned and got a new character. Thus, even though the player got a new character with a new sparkly Humanoid
, the program was still referencing the original. The solutionâs to use CharacterAdded
to get the new character and grab the new Humanoid
.
EDIT
Thanks for selecting my solution!
yours made more sense and you were the first one to do it
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Frame = script.Parent
local Health = Frame:WaitForChild("Health")
local function OnCharacterAdded(Character)
local function OnHealthChanged(Health)
Health.Text = math.round(Health)
end
local Humanoid = Character:WaitForChild("Humanoid")
Health.Text = Humanoid.Health
Humanoid.HealthChanged:Connect(OnHealthChanged)
end
Player.CharacterAdded:Connect(OnCharacterAdded)
You donât need tostring()
and you donât need to disconnect the created connections, they are automatically disconnected when the humanoid instance is destroyed.
We should also add a WaitForChild()
call on the GuiObject named âHealthâ to allow for it to replicate to the client.
so? should i use yours or could i just use his which one would be better?
It doesnât really matter which you use, I was just providing a few pointers.
Interesting that tostring
isnât required. Iâm iffy about that second point however since you can bring the old character âback to lifeâ (meaning it isnât Destroy
ed, thus you can bring the original character back. This is why the following codeâs possible).
Player.CharacterRemoving:Connect(function(OldCharacter)
OldCharacter.Parent = game.Workspace
end)
When a playerâs characterâs removed it is not Destroy
ed. To make sure the characterâs destroyed, youâd have to use the above event (CharacterRemoving
), pass the old character to the function and Destroy
it.
Player.CharacterRemoving:Connect(function(OldCharacter)
OldCharacter:Destroy()
end)
Note that this code has to used in a (server) script and not a (client) localscript to have its being Destroy
ed replicated to all clients, the server included. Otherwise only using it in the latter will only destroy it on that client and not the server nor other players.
â
EDIT
I never said it did. As I explained prior, it fires when the old playerâs characterâs removed and passes it to a function. I can kinda see where you might have gotten that impression, but itâs a stretch imo. Added a few points for clarification.