✓ Accepted answer
This happens because .Text is a string and CurrentAmmo and MaxAmmo are IntValues or (NumberValues) meaning that their .Value are numbers. According to Lua, strings and numbers are never equivalent. Meanwhile, Roblox allows you to assign a number to .Text and it will perform tostring on the number automatically.
To fix the problem, you need to make sure to compare identical types. You can either call tostring on the numbers or tonumber on the strings. Comparing strings may be the better option:
local CurrentAmmo = script.Parent.CurrentAmmo
local MaxAmmo = script.Parent.MaxAmmo
local config = script.Parent.Parent.Parent.Parent.Backpack.AWP.Config
while true do
if CurrentAmmo.Text ~= tostring(config.CurrentAmmo.Value) then
CurrentAmmo.Text = config.CurrentAmmo.Value
print("changed current ammo")
end
if MaxAmmo.Text ~= tostring(config.MaxAmmo.Value) then
MaxAmmo.Text = config.MaxAmmo.Value
print("changed max ammo")
end
wait()
end
Note that it is far better to listen to the .Changed event of those Values rather than using a loop:
local CurrentAmmo = script.Parent.CurrentAmmo
local MaxAmmo = script.Parent.MaxAmmo
local config = script.Parent.Parent.Parent.Parent.Backpack.AWP.Config
config.CurrentAmmo.Changed:Connect(function()
CurrentAmmo.Text = config.CurrentAmmo.Value
end)
config.MaxAmmo.Changed:Connect(function()
MaxAmmo.Text = config.MaxAmmo.Value
end)
Unlike the events, the loop will run even when you don't need it to, wasting processing time (although you wouldn't notice the difference in this case).
3 comments
btw this is a local script inside of frame
i think it is because it is in a while true loop without a wait. in the wait() function you didn't put a num. Maybe putting a wait for 5 secs will work?
no it doesn't work if i put 5 second that will be a problem because if it changes it could potentially take 5 seconds to update the change