How to make a local value automatically update when a value is changed

Hello,
I was trying to script something where when you put an item ID from roblox into a text box, it sends that ID to a value inside a folder in the player, and from there, there is a script inside of the screengui of playergui that allows the text boxes inside of the screengui to get information from that ID. The problem is that the script only runs one time (when the player joins the game) and does not update when the player puts a new ID. By default, the value is set to nothing. Tried wrapping everything in a while wait() do loop and still nothing. Here is some of the script:

local player = game.Players.LocalPlayer
local gui = player.PlayerGui.InfoProvider.Frame
local Asset = wait(.05) do game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value)

I just want the value of player.leaderstats.ItemID.Value to update when the player changes it, or update every .05 seconds.
Thanks!

1 Like

You can do something like

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
      game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value)
end)

Or let’s say you want the text box to update after the player is no longer focused in the text box, you could do something along the lines of:

TextBox.FocusLost:Connect(function()
       game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value)
end)

OR since you mentioned looping every 0.5 seconds, you can use a while loop to get this:

while wait(0.5) do
   game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value)
end
3 Likes

I need the game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value) to be a local value though, or the script won’t work.

Do you mean setting it as a variable? If so, do:

local ProductInfo = game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value)

But I need the variable to change when the user inputs a new id. game:GetService(“MarketplaceService”):GetProductInfo(player.leaderstats.ItemID.Value) <<< I need that part to update.

local ProductInfo = game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value)

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
   ProductInfo = game:GetService("MarketplaceService"):GetProductInfo(tonumber(TextBox.Text))
end)

Still nothing…
Would player.leaderstats.ItemID.Value:GetPropertyChangedSignal work?

You can actually do

player.leaderstats.ItemID:GetPropertyChangedSignal()

Make sure to use the Instance and not the Value!!!

1 Like

I’m just going to show you the whole script:

local gui = player.PlayerGui.InfoProvider.Frame
local Asset = game:GetService("MarketplaceService"):GetProductInfo(player.leaderstats.ItemID.Value)
gui.ItemID:GetPropertyChangedSignal("Text"):Connect(function()
	Asset = game:GetService("MarketplaceService"):GetProductInfo(tonumber(gui.ItemID.Text))
end)
gui.ItemDescription.Text = Asset.Description
gui.ItemCreator.Text = Asset.Creator
gui.ItemFree.Text = Asset.IsPublicDomain
gui.ItemName.Text = Asset.Name
gui.ItemLimited.Text = Asset.IsLimited
gui.ItemLimitedU.Text = Asset.IsLimitedUnique
gui.ItemSales.Text = Asset.Sales
gui.ItemPrice.Text = Asset.PriceInRobux
gui.ItemCreated.Text = Asset.Created
gui.ItemNew.Text = Asset.IsNew
gui.ItemsRemainU.Text = Asset.Remaining

It still does not work

Oh wait hold up
I forgot something
EDIT: Nevermind. player.leaderstats.ItemID:GetPropertyChangedSignal() is not an actual “function” in roblox. It doesn’t put an end on the end of the player.leaderstats.ItemID:GetPropertyChangedSignal() when I hit enter.

I actually have no clue how to do this anymore. I guess I’ll just modify one of your scripts until I get it to work. Thank you, @ijmod!

1 Like

Of course! What you could do to optimize your code is wrap the following code in a function and updating it when the value changes. Your code is only executing at the start of the game and not when anything is changing.

local function ChangeText()
gui.ItemDescription.Text = Asset.Description
gui.ItemCreator.Text = Asset.Creator
gui.ItemFree.Text = Asset.IsPublicDomain
gui.ItemName.Text = Asset.Name
gui.ItemLimited.Text = Asset.IsLimited
gui.ItemLimitedU.Text = Asset.IsLimitedUnique
gui.ItemSales.Text = Asset.Sales
gui.ItemPrice.Text = Asset.PriceInRobux
gui.ItemCreated.Text = Asset.Created
gui.ItemNew.Text = Asset.IsNew
gui.ItemsRemainU.Text = Asset.Remaining
end

and execute it under a TextBox.FocusLost event to have it update every time :slight_smile: