Change TextLabel when NumberValue changes

How can I change TextLabel text when a NumberValue changes? Like if a NumberValue’s value is 10 the TextLabel’s text would would change to 10, and if a NumberValue’s value is 20 the TextLabel’s text would change to 20, and so on.

3 Likes

Well whenever a value of any sorts changes for any object it fires a .Changed event. And as a Number value only has 1 value then the changed event will fire when that 1 value gets changed. So you can just do

NumberValue.Changed:Connect(function()


textlabel.text = NumberValue.Value

end)

--syntax may be a bit off as a I on mobile

2 Likes


Didn’t work sadly… as you can see in the video, if I change the NumberValue to 1,000, the TextLabel doesn’t change to 1,000. heres the script I used:

local GiftbuxNumberValue = game.StarterGui.ScreenGuis.GiftbuxNumberValue
local GiftbuxTextLabel = game.StarterGui.ScreenGuis.Giftbux.GiftbuxBalance

GiftbuxNumberValue.Changed:Connect(function()

	GiftbuxTextLabel.Text = GiftbuxNumberValue.Value

end)

--syntax may be a bit off as a I on mobile
2 Likes

I see a lot of people making this mistake, but it isn’t game.StarterGui.
Starter GUI is replicated inside the player’s gui whenever he joins/respawns. Accessing it this way you are overriding the default for whenever a player spawns or joins. To actually access the player’s actual GUI you must do game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui').
This actually will use the player’s UI on his screen. Make sure it is a local script and you are good to go. (never script UI on the server, that’s stupid and expensive)
Read the documentation of PlayerGui here

Edit:
Sounds very complex, but consider learning a UI framework like fusion or roact. There are many more out there and they support reactive/declarative statements. This will help you learn, be more productive and make higher quality products. Even if you are new learning it would benefit in the future, but it’s up to you. This is only a suggestion.

2 Likes

So, like this? Sorry I’m new to scripting…

game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')


local GiftbuxNumberValue = game.PlayerGui.ScreenGuis.GiftbuxNumberValue
local GiftbuxTextLabel = game.PlayerGui.ScreenGuis.Giftbux.GiftbuxBalance

GiftbuxNumberValue.Changed:Connect(function()

	GiftbuxTextLabel.Text = GiftbuxNumberValue.Value

end)

--syntax may be a bit off as a I on mobile
1 Like

No, not really. I see you are pretty new to scripting. local GiftbuxNumberValue = something is a variable storing that object. When you refer to game.PlayerGui you are referring to that, adding the line I sent you on top changes nothing. You need to use that line as a variable and replace what you have. It will look something like this:

local playerGui = game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')


local GiftbuxNumberValue = playerGui.ScreenGuis.GiftbuxNumberValue
local GiftbuxTextLabel = playerGui.ScreenGuis.Giftbux.GiftbuxBalance

GiftbuxNumberValue.Changed:Connect(function()
	GiftbuxTextLabel.Text = GiftbuxNumberValue.Value
end)

You must do the same for when changing the value. I see it is inside your UI, so make sure you do that for the value changing script. Also, a very easier method would be to simply put your script inside the UI and referring to the objects with script.Parent.

2 Likes

I get an error saying “Players.SleepyUnnamed.PlayerGui.GiftbuxHandler.NumValChanger:7: attempt to index number with ‘Changed’”

Here’s the LocalScript:

local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")


local GiftbuxNumberValue = PlayerGui.GiftbuxHandler.GiftbuxNumberValue.Value
local GiftbuxBalance = PlayerGui.GiftbuxHandler.GiftbuxFrame.GiftbuxBalance

GiftbuxNumberValue.Changed:Connect(function()

	GiftbuxBalance.Text = GiftbuxNumberValue.Value

end)

--syntax may be a bit off as a I on mobile

Here’s my explorer:
image

1 Like

Oh, sorry didn’t notice that mistake you did. The .Changed event fires whenever ANY property on an instance changes, so calling it on a .Value won’t work. You must use GetPropertyChangedSignal. Look the post and see how it’s done, but simply:

local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")

local GiftbuxNumberValue = PlayerGui.GiftbuxHandler.GiftbuxNumberValue
local GiftbuxBalance = PlayerGui.GiftbuxHandler.GiftbuxFrame.GiftbuxBalance

GiftbuxNumberValue:GetPropertyChangedSignal("Value"):Connect(function(newValue)
	GiftbuxBalance.Text = newValue
end)

If I’m not mistaken this should work. When working with .Changed the function takes the changed property as an argument, but you don’t know if it’s the correct one so you must use an if statement to check, but using getpropertychangedsignal you don’t need that, you already specify the property you are checking for.

1 Like

You used . instead of :, so it might error. Also, for instances of Value type, it is recommended you use .Changed event.

1 Like

My bad, auto-complete is not here to save me, haha.
Both of these events are pretty much the same and have the same use case; one is just for checking any value, and the other is for a specific value. It’s up to preference here. I get that a value object has no other properties to change, but what if it happens? Your script will error. I don’t want to say one is better than the other; again, it’s up to preference and use case. I doubt there is any benefit; I just prefer to do get property changed signal.

1 Like

It didn’t work, changing the NumberValue did not change the TextLabel, I also did not get any errors as far as I’m aware.

local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")

local GiftbuxNumberValue = PlayerGui.GiftbuxHandler.GiftbuxNumberValue
local GiftbuxBalance = PlayerGui.GiftbuxHandler.GiftbuxFrame.GiftbuxBalance

GiftbuxNumberValue:GetPropertyChangedSignal("Value"):Connect(function(newValue)
	GiftbuxBalance.Text = newValue
end)
1 Like

1 Like

Again, are you sure that the script changing that value also uses the players gui and not the startergui? Can you show that script?
Try adding this as an example to see if it changes: (it should)

task.wait(5)
GiftbuxNumberValue.Value = 50
print("value changed")

Also @SomeFedoraGuy I get that and I know this exception for the .Changed event, I prefer get property changed signal just because I remember it better. In her original script she did the .Changed event on object.Value when it should be only the object.

1 Like

Uhhh =P

script.Parent.MouseButton1Click:Connect(function()
	game.StarterGui.GiftbuxHandler.GiftbuxNumberValue.Value = 1000
end)
1 Like

Yeah you did the same mistake, just replace it with what we did earlier and you are fine.
i hope

1 Like

I get an error saying “Unable to assign property Text. string expected, got nil” :sob:

local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")

local GiftbuxNumberValue = PlayerGui.GiftbuxHandler.GiftbuxNumberValue
local GiftbuxBalance = PlayerGui.GiftbuxHandler.GiftbuxFrame.GiftbuxBalance

GiftbuxNumberValue:GetPropertyChangedSignal("Value"):Connect(function(newValue)
	
	GiftbuxBalance.Text = newValue
end)

1 Like

Oh my god I’m so sorry, I literally woke up an hour ago and my brain isn’t working. Get property changed signal doesn’t have an argument. Sorry for that.

GiftbuxNumberValue:GetPropertyChangedSignal("Value"):Connect(function()
	GiftbuxBalance.Text = GiftbuxNumberValue.Value
end)
1 Like

Finally it worked dammit! Thank you so much.

2 Likes

Alright; lets go through this.


First, @iceeburrs is fully correct… they deserve to get the answer for this.

Let’s go over something

StarterGui

Everything that is placed in the StarterGui folder, is copied to each player; and put into their PlayerGui folder. This is located inside of the Players profile.
image

This is working, however it’s only changing the value in the StarterGui; which isn’t what each player will see.

Solution

I made a very easy fix, and quick fix for this; but this will need updated if you try changing it for different players, rather than just one player. Here’s a photo showing how I have everything set-up.

local MainValue,Label,Button = script.Parent.Value,script.Parent.TextLabel,script.Parent.TextButton

MainValue.Changed:Connect(function(Re)
	Label.Text = MainValue.Value
end)

Button.MouseButton1Click:Connect(function()
	MainValue.Value = MainValue.Value + 5
end)

Fixed.rbxm (8.5 KB)


damn, a few minutes late.

2 Likes

finally :skull:
again i’m sorry for that mistake I did, I am a little tired, shouldn’t have went here to try help people when I can’t think correctly.
also I hope you got the difference between those 2 events and the exception for .Changed for value instances.
also look at what @Just2Terrify just wrote, that is pretty well explained.

2 Likes