Updating A textLabel After A Purchase

This script shows the price of an item to the player through a textLabel. After the player buys an item the textLabel does not update to the new price.

local upgrades = script:WaitForChild('UpgradesIntValue')  ---UpgradesIntValue is an IntValue under StarterPlayer and it’s value is set to 1

local increase = 1.5

while true do

script.Parent.Text = upgrades.Value*increase

end

If you’re using a loop without any yield, Roblox stops it. Try adding a yield like wait.

Example:

local upgrades = script:WaitForChild('UpgradesIntValue')  ---UpgradesIntValue is an IntValue under StarterPlayer and it’s value is set to 1

local increase = 1.5

while true do

    script.Parent.Text = upgrades.Value*increase

    wait(1)

end
2 Likes

I dont know what your trying to do but for the loop maybe try adding a wait()

1 Like

Worth saying: you should probably opt to use an event here instead of a loop. If the UpgradesIntValue is the one changing and you want to track the value of that, I suggest using the Changed event. This way, the code that updates the text only ever runs when the value changes, not constantly.

Always try to use event-based approaches in your system where possible. Only use a loop if it’s necessary that you do so and where RunService may not suffice or be desirable for your use case.

local upgrades = script:WaitForChild("UpgradesIntValue")
local increase = 1.5

local function updateText(newValue)
    script.Parent.Text = newValue * increase
end

upgrades.Changed:Connect(updateText)
updateText(upgrades.Value)
3 Likes