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

My event wont fire in my Health Bar code and there are no errors, what's wrong with this code?

Hi, I'm trying to create a healthbar that detects when the player takes damage on the server, and changes the UI on the client. Here is my code. I get no errors, and nothing happens.

Serverscript in serverscriptservice:

local players = game("GetService", "Players")
local replicatedStorage = game("GetService", "ReplicatedStorage")
local events = replicatedStorage.Events
local event = events.HealthEvent
players.PlayerAdded:connect(function(player)
	print(player.ClassName)
	local gui = player:WaitForChild("Backpack")
	print(gui)
	local scriptOne = gui:WaitForChild('HealthHandler')
	player.CharacterAdded:connect(function(character)
		character.Humanoid.HealthChanged:connect(function(player)
			event:FireClient(player)
			end)
	end)
end)

Local script in StarterPack

local players = game("GetService", "Players")
local replicatedStorage = game("GetService", "ReplicatedStorage")
local events = replicatedStorage.Events
local event = events.HealthEvent
local player = players.LocalPlayer
event.OnClientEvent:Connect(function()
	print("hi")
	player.CharacterAdded:connect(function(character)
		print(character)
		local health = character.Humanoid.Health/100
		player.PlayerGui.HealthUI.HealthFrame.Healthbar.Size = UDim2.new(health,0,1,0)
	end)
end)

2 comments

0

Why check the player's health on the server? Unless you want to check and display every player's health, I don't see a point.

0

Thank you tho!

1 answer

I believe that the issue is that you wait on the Player's Character to spawn twice; your server side function waits for a Character to be added, and the event you call does the same. Since you aren't adding the Character twice, the function (client side) never runs.

Also notable is that running a server side handler for Player health seems ridiculous.

In a LocalScript (now tested and working):

local player = game.Players.LocalPlayer;
local gui = player.PlayerGui:WaitForChild("HealthUI").HealthFrame.Healthbar;

local c = player.Character;
c.Humanoid.HealthChanged:connect(function(newhealth)
	gui.Size = UDim2.new(newhealth/c.Humanoid.MaxHealth, 0, 1, 0);
end)

5 comments

0

Im new to filtering enabled, perhaps i didnt know the best system.

0

It works! Thank you !!!

0

You're welcome. FilteringEnabled (and experimental mode) exist to prevent players from exploiting. Since we shouldn't care what the player's health is, just the percentage of their health they have left, we can just check their health/maxhealth on the client. :)

0

The script stops working whe nteh character dies uh oh

0

Try replacing the line "local c = player.Character" with player.CharacterAdded:connect(function(c) ... end), where ... starts at c.Humanoid and ends at end)

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