How to get IntValue and apply it to TextLabel in LocalScript?

Hello. I’m trying to make a BillboardGui with a TextLabel that gets a IntValue inside of the player. Something is wrong with my function.

local player = game:GetService("Players")
local localplayer = game:GetService("Players").LocalPlayer
local textLabel = script.Parent
local value = localplayer.LotteryFolder.LotteryWins.Value


player.PlayerAdded:Connect(function(plr)
	textLabel.Text = ("Lottery wins: ".. (plr.LotteryFolder.LotteryWins.Value))
end)

Pictures of explorer:
image
image
Adding a print function inside doesn’t print anything either.
Any and all help is very much appreciated.

4 Likes

Are there any errors? You said that you have tried to add a print statement. Where did you put the print statement? Did you put it in the event function? If it didn’t trigger, are you sure that the script isn’t disabled? Try putting another print statement at the start of the script.

2 Likes

Script isn’t disabled. No errors either. Neither the print inside the function and outside the function work.
Could it be because .PlayerAdded can’t run on LocalScripts?

2 Likes

No, PlayerAdded doesn’t work in a LocalScript. You should just be able to do localplayer.LotteryFolder to access the folder.

So the code would look like:

local players = game:GetService("Players")
local localplayer = players.LocalPlayer
local textLabel = script.Parent
local value = localplayer.LotteryFolder.LotteryWins.Value

textLabel.Text = "Lottery wins: " .. value
3 Likes

Ah, I see. Even when removing the .PlayerAdded function the text doesn’t update. Is it because the script just doesn’t have a way to run?

2 Likes

I would recommend moving the script to StarterPlayerScripts, assuming you want the object in the workspace to display a different value for each player (whatever their value is in their LotteryFolder.

Make sure to also change the textLabel to the actual path of the label, since the script would no longer be under the label.

4 Likes

Also I was mistaken, PlayerAdded only triggers in a LocalScript when a different player is added to the game but it ignores the player that is running the script.

3 Likes

Oh right. I forgot that whenever I want to change GUI text with a LocalScript the script cannot be parented to whatever textlabel/box i’m trying to set but instead putting it into StarterGui or StarterPlayerScripts will make it work. Thank you so much.

3 Likes

I’m afraid I’ve ran into an issue. The script only changes the value to 0 but doesn’t update.
edit: i’m pretty sure i pathed the value wrong

1 Like

You want it to keep updating?

You could use a .Changed event to detect when the value changes and update it when it changes.

Here is what the script would look like:

local players = game:GetService("Players")
local localplayer = players.LocalPlayer
local textLabel = script.Parent
local lotteryWins = localplayer.LotteryFolder.LotteryWins

lotteryWins.Changed:Connect(function()
    textLabel.Text = "Lottery wins: " .. lotteryWins.Value
end)
2 Likes

You changing lotterywins into lotterywins.Value in the textlabel.Text fixed the issue. Thank you so much once again.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.