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:
01 | local CurrentAmmo = script.Parent.CurrentAmmo |
02 | local MaxAmmo = script.Parent.MaxAmmo |
03 | local config = script.Parent.Parent.Parent.Parent.Backpack.AWP.Config |
06 | if CurrentAmmo.Text ~ = tostring (config.CurrentAmmo.Value) then |
07 | CurrentAmmo.Text = config.CurrentAmmo.Value |
08 | print ( "changed current ammo" ) |
11 | if MaxAmmo.Text ~ = tostring (config.MaxAmmo.Value) then |
12 | MaxAmmo.Text = config.MaxAmmo.Value |
13 | print ( "changed max ammo" ) |
Note that it is far better to listen to the .Changed
event of those Values rather than using a loop:
01 | local CurrentAmmo = script.Parent.CurrentAmmo |
02 | local MaxAmmo = script.Parent.MaxAmmo |
03 | local config = script.Parent.Parent.Parent.Parent.Backpack.AWP.Config |
05 | config.CurrentAmmo.Changed:Connect( function () |
06 | CurrentAmmo.Text = config.CurrentAmmo.Value |
08 | config.MaxAmmo.Changed:Connect( function () |
09 | MaxAmmo.Text = config.MaxAmmo.Value |
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).