leaderstats.Health == 3 then it affects Hearts ImageLabel UI

Hello Developers!
I’m sorry for posting two topics in one day.

Basically, I’m trying to achieve something as shown in the picture below:


When you click on the wrong option you lose a point. I’ve already figured that out. But, now I’m trying to make it affect the UI.

(The script is in ServerScriptService.)
I’ve tried to make something among the lines of this:

function verifyHealth(player)
	if player.leaderstats.Health.Value == 3 then
		script.Parent.Parent.StarterGui.BackgroundMain.Heart1.Visible = false
	else return "NAN" end
end

game.Players.PlayerAdded:Connect(function(plr)
    plr:WaitForChild("leaderstats")
	if verifyHealth(plr) == "NAN" then 
		plr.leaderstats.Health.Changed:Connect(function()
			verifyHealth(plr)
		end)
	end
end)

But, I’d always get this in the output and would never have it work.

E1

Even though… I’ve checked and found out that it was a valid member…

E2

So, I am very confused in how to make it so when it hits a certain value like value = 3 or value = 2; then it affects the GUI and changes the heart.
Thank you for taking time to read this. I might not respond now, but I will do that later.
Have a great day!

2 Likes

A few notes:

image
here, this line won’t do anything unless you’re calling it.

if I understood you right, you want to make the heart gui syncs with the player’s Health Value?

2 Likes

Basically, that. If the Heart Value is at a certain point then it does affect the Heart GUIs so yeah.
But, I do not understand why I got the Error in Output if nothing seems wrong.

You don’t get any warnings here? (Such as “No child blablabla found”)
Usually when calling :WaitForChild() you would save the result (aka “leaderstats”).
Consider changing this to

local leaderstats = plr:WaitForChild("leaderstats")

This will also improve readability and make things easier for you.

2 Likes

You could have a frame with 3 hearts, and each of them has[as their child - another image, which will be a broken heart - make sure that child is invisible yet].

What you could do, lets say a player begins with 3 hearts,
you could name each of those UI hearts in numbers [1,2,3] and with an ipairs inside a function/event, you could reduce -1 [As long as the player has more than 0 Hearts - Health, meaning, if the player has lost all his Health, then you stop the loop or make an if statement inside of a function]

And you could connect that with his hearts UI, for e.g :

For every heart he losses [meaning he’s lost -1 from his Health], you could hide the ‘Healthy image’ and show up the ‘Broken Heart’ on that spot.

I know it sounds a bit confusing, but if you didnt understand,

I’ll try to explain better soon [ gotta go ].

1 Like

One potential issue here that I see that might also hinder your progress is:
script.Parent.Parent.StarterGui.BackgroundMain.Heart1.Visible = false

You can think of StarterGui as kind of a container. Whenever someone joins the game, that container is “cloned” and then put into the Players PlayerGui. Any changes you make to the StarterGui wont effect anything! Instead you can try the following:

player.PlayerGui.BackgroundMain.Heart1.Visible = false

Although, this won’t seem to solve your main issue here. I helped you with the previous script and I’m going to go with the assumption that you’re still using that! Let’s take a look back at that server script from the previous post and make a few edits.

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
	if plr.leaderstats.Health.Value > 0 then
	plr.leaderstats.Health.Value = plr.leaderstats.Health.Value - 1
	local Value = plr.leaderstats.Health.Value
	plr.PlayerGui.BackgroundMain:FindFirstChild("Heart"..tostring(Value + 1)).Visible = false
	end

end)

Edit: Made a quick edit to make sure that last heart goes away. Try this instead.

1 Like