Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

if statements are running for no reason?

Asked by 3 months ago

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?

0
btw this is a local script inside of frame botw_legend 452 — 3mo
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? Lightning_Game27 218 — 3mo
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 botw_legend 452 — 3mo

1 answer

Log in to vote
1
Answered by 3 months ago

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).

0
ohh wow you have over 5,000! i have never seen anybody over 1,000! botw_legend 452 — 3mo
0
wait... you get 15 reputation from one question how do u have that much??? botw_legend 452 — 3mo
0
According to my profile, "667 answers given, 341 accepted" =) chess123mate 5408 — 3mo
Ad

Answer this question