Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

if statements are running for no reason?

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?

3 comments

0

btw this is a local script inside of frame

0

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?

0

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

1 answer

✓ 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

0

ohh wow you have over 5,000! i have never seen anybody over 1,000!

0

wait... you get 15 reputation from one question how do u have that much???

0

According to my profile, "667 answers given, 341 accepted" =)

This discussion is preserved as it was. New answers, comments, and votes are closed.