How would I detect a player when they lose health but on ServerSide?

LOCAL SCRIPT

wait(1)
while wait() do
	if Character.Humanoid.Health == 100 then
		script.Parent.Parent.Parent.Visible = false
	end
	if Character.Humanoid.Health <= 99 then
		script.Parent.Parent.Parent.Visible = true
	end
	script.Parent.Size = UDim2.new(Character.Humanoid.Health / Character.Humanoid.MaxHealth, 0, 1, 0)
end

How would I use this for serverside instead of only the client seeing their bar change?

1 Like

Do it with a event, client - - server

1 Like

Can you provide an example??? I don’t know how a serverscript would be able to modify a GUI. (i tried modulescript but it didnt work)

There is an article on DevHub on remote events

Your trying to modify a billboard gui right?

A textlabel under a overhead GUI, yes.

You can send the parameter player through the remote event and modify that player’s textlabel to anything you want

Can I see an example? I’m not very good at remotevents.

You can try using Humanoid.HealthChanged in both your local and serverscript to minimize the use of loops
https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

2 Likes
game.ReplicatedStorage.Event.OnServerEvent:Connect(function(player) 
    - - gui code 
end) 
1 Like

Is this a serverscript or a localscript?

You are receiving the event from the client so is a server script.

You could use remote events to replicate, or use PlayerAdded and CharacterAdded in a server script to handle the health serverside

Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function(property)
-- code here (Example, print(property) -- prints the health after it was changed)
end)

hope thiz helpz

this would also run when the player heals it would work if there was no healing in his game

Are you trying to make a healthbar? If so, it is possible to handle health bars entirely on the client, rather than rendering them on the server, using character added and health changed events.

https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

-- script inside serverscriptservice

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.HealthChanged:Connect(function(health)
            print(player.Name, "has", health, "health")
        end)
    end)
end)

you can also do the same on the client side you don’t need a while loop

-- localscript inside startcharacterscripts

local playerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function(health)
    print("LocalScript", health)
    playerGui.HealthBar.Size = UDim2.new(health / humanoid.MaxHealth, 0, 1, 0)
end)
local PrevHealth = 100
Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function(property)
    if PrevHealth < property then return end PrevHealth = property
    
end)