Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make my lives gui update to the value of lives?

Asked by 4 years ago
Edited 4 years ago

I'm working on a game that uses a lives system. The lives and resetting you to the begging part works fine, but I have a gui I want to display the number of lives you have left. It currently doesn't change upon death and always shows the value of the text as it is before playing, which is "Value". Is there some kind of way to update it upon the value changing?

Screen snip:

https://imgur.com/a/nJc4DHH

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

How you're currently doing it wont work because you're changing the gui in the startergui instead of the player gui.

When the player joins, anything inside of the StarterGui gets copied into the player's player PlayerGui (game.Players.LocalPlayer.PlayerGui)

To update the gui whenever the value changes, you can use a value object's .Changed. .Changed runs whenever the value object (in this case, Lives)'s value gets changed.

Directly inside the HUD gui, have a localscript containing the following:

local livesValue = game.Players.LocalPlayer:WaitForChild("Lives") --here i'm making the I'm the assumption that the lives value object is inside the player. let me know if it's not

local livesText = script.Parent.Lives.LivesValue

livesValue.Changed:Connect(function(newValue)
    livesText.Text = tostring(newValue)
end)
0
I just get an error saying "Lives is not a valid member of Frame" shipleydog2 -3 — 4y
0
I tried it in the HUD gui and it didn't change anything, it didn't work. I dunno why I put it in the lives frame. Anyway, upon doing it in the hud it didn't have the value in the gui. shipleydog2 -3 — 4y
0
The error I got upon putting it in the HUD was "Players.shipleydog2.PlayerGui.HUD.LocalScript:6: attempt to index upvalue 'livesText' (a string value)" shipleydog2 -3 — 4y
0
oop that was typo on my part ill edit it royaltoe 5144 — 4y
View all comments (5 more)
0
Alright. shipleydog2 -3 — 4y
0
It still doesn't work. I'm not getting any errors, either. shipleydog2 -3 — 4y
0
would you be okay with team creating and or talking over discord? would be easier to debug there royaltoe 5144 — 4y
0
Sure, that's fine. shipleydog2 -3 — 4y
0
Lucy#0003 royaltoe 5144 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

This is not so hard, you need to use the "Changed" event, here's an example script for you.

local LifeCount = game.Players.LocalPlayer.WaitForChild("Lives")

local LifeCountText = --Refer to the Text here

LifeCount.Changed:Connect(function()
    LifeCountText.Text = "Lives: " .. LifeCount.Value -- Whenever the LifeCount changes, the text will be updated
end)

Answer this question