so this is my code:
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 ~= config.CurrentAmmo.Value then CurrentAmmo.Text = config.CurrentAmmo.Value print("changed current ammo") end if MaxAmmo.Text ~= config.MaxAmmo.Value then MaxAmmo.Text = config.MaxAmmo.Value print("changed max ammo") end wait() end
when i run it, it spams the print statements because it keeps on running it is suppose to change it so it is the same why is this happening?
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).