Why won't this text update/change

Hello!

I am having some troubles with the script that’s in my text.
Screenshot 2024-01-22 012850

The script inside both of the text’s (first one being on the top where it shows 730/5500 and the second one being the same, but in the menu) works, but not to the extent I want it to. Basically, the value on the right (5500) is the required amount of XP for the player to level up and it changes once you leave and rejoin, but when you actually level up, the amount changes in the script, but it doesn’t show the change in the UI. For example, they need 5500 to level up, but once they level up their amount changes to 6000, but it still shows 5500.

Here’s the script:

Top image:

local player = game.Players.LocalPlayer

local levelStats = player:WaitForChild("LevelStats")
local level = levelStats:WaitForChild("Level")
local XP = levelStats:WaitForChild("XP")

local neededStr = (level.Value + 1) * 500

script.Parent.Text = ""..XP.Value.."/"..neededStr..""

local function update()
	script.Parent.Text = ""..XP.Value.."/"..neededStr..""
end

XP.Changed:Connect(function()
	update()
end)

level.Changed:Connect(function()
	update()
end)

update()

Bottom Image:

local player = game.Players.LocalPlayer

-- values

local levelStats = player:WaitForChild("LevelStats")
local str = levelStats:WaitForChild("XP")
local level = levelStats:WaitForChild("Level")

local neededStr = (level.Value + 1) * 500

script.Parent.Text = "".. str.Value .."/".. neededStr ..""

level.Changed:Connect(function()
	script.Parent.Text = "".. str.Value .."/".. neededStr ..""
end)

str.Changed:Connect(function()
	script.Parent.Text = "".. str.Value .."/".. neededStr ..""
end)

Anything will help, feel free to ask questions! Thanks! :slight_smile:

It seems like the neededStr variable is being calculated outside of the function and not being updated properly, if the player levels up the script is still using the old level value. Try putting this line in the update function:

local function update()
	local neededStr = (level.Value + 1) * 500
	script.Parent.Text = ""..XP.Value.."/"..neededStr..""
end

Also, put an update function in your bottom image script as it has less clutter.

local function update()
	local neededStr = (level.Value + 1) * 500
	script.Parent.Text = "".. str.Value .."/".. neededStr ..""
end

level.Changed:Connect(update)

str.Changed:Connect(update)

update()
1 Like

This isn’t a function. This is a declaration. That means Roblox will compute the result once, and then it will do nothing. So every time you try to use the value, you’re using the initial value.

To fix, move all the data inside the update function.

1 Like

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