GUI not vanishing in server script

Hello!

I am trying to make a health system that keeps track of the player’s health. But when I test the script the Heart doesn’t disappear.

Script:

local Heart1 = game.StarterGui.HeartGUI.Heart1
local Heart2 = game.StarterGui.HeartGUI.Heart2
local Heart3 = game.StarterGui.HeartGUI.Heart3
local Heart4 = game.StarterGui.HeartGUI.Heart4
local Heart5 = game.StarterGui.HeartGUI.Heart5

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")
		if humanoid.Health > 90 then
			Heart5.ImageTransparency = 1
		end
	end)
end)

Couple of things:

  • Changes in game.StarterGui only affect new players. You should instead do plr.PlayerGui.

  • Whenever possible, you should use a local script to change UI elements,.

1 Like

Can you alter the script to make those changes please

I also tried a local script to change the ui but the function didnt work when it detects humanoid

Server script (not recommended)

game.Players.PlayerAdded:Connect(function(plr)
    local Heart1 = plr.PlayerGui.HeartGUI.Heart1
    local Heart2 = plr.PlayerGui.HeartGUI.Heart2
    local Heart3 = plr.PlayerGui.HeartGUI.Heart3
    local Heart4 = plr.PlayerGuiHeartGUI.Heart4
    local Heart5 = plr.PlayerGui.HeartGUI.Heart5
	plr.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")
		if humanoid.Health > 90 then
			Heart5.ImageTransparency = 1
		end
	end)
end)

Local script (with some other changes)

local player = game.Players.LocalPlayer

local function CharacterAdded(char)
    char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
        local Health = char.Humanoid.Health
        if Health > 90 then
            plr.PlayerGui.Heart5.ImageTransparency = 1
      end
  end
end

CharacterAdded(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(CharacterAdded)

The problem here is that you are referencing the StarterGui. Things in StarterGui are cloned over to the client’s PlayerGui, which is located in the player and any changes to StarterGui will not be seen by the player:

image

Are you trying to modify the client’s GUI from a server script or a local script?

Anything UI related should be handled by the client and the server must send any necessary information over through remote events, or the client requests it through remote functions.

Since the player’s character is controlled by the client we can use a local script like what @SeargentAUS did there.