# How to check a value in a loop

Hey guys! I’m making a quest for my game, it’s simple: get 5 gifts and earn a reward

However, for the progress gui, i want to make a loop, that checks everytime the value for the transparency/image color be 0, but everytime that i enter in the game, i check the outpost and it’s filled with “”

The script that i wrote:

``````while true do
else
end
else
end
else
end
else
end
print("hooray!")
player.Tokens.Value = player.Tokens.Value + 1240
else
end
end)
end
``````

If anyone can help me, thank you!

You don’t need to call this multiple times, especially that you’re nesting it inside the infinite loop.

Try hooking functions to `gifts.Gifts`, where `gifts.Gifts.Changed` will react upon change of the value… It’s generally more performant than looping infinitely…

Yeah… i never used “Changed” before, lol.
But thanks for the advice, gonna try that later.

I would recommend use of a table. Dealing with numbers, this could be either an array or dictionary

Here is an example for what that might look like (with an array, assuming the gift# will never be 0)
(Also assumes each Gift(1-x) has a IntValue with the number associated with it)
Lord forgive me for this code block

``````local giftTable = {
Color3.new(x,x,x), --This is index one
Color3.new(x,x,x), -- This is index two
Color3.new(x,x,x), -- This is index three
... -- And so on and so forth
}

gift.ImageColor3 = giftTable[x] -- this will set the color3 to the associated color3 in the array at index x.
else
end
end
``````

This could work, and also may be an introduction to for loops. (albeit, not the best introduction )
It also allows for ease of scaling.
Hope this helps.

2 Likes

At the initial stage of the solution, I was looking at what it would look like in a refactor:

``````local Players = game:GetService("Players")

-- Refactored, right implementation but wrong idea?
local tokens = player:WaitForChild("Tokens")

if newValue > 0 and newValue <= 5 then

if newValue == 5 then
tokens.Value += 1240
end
else
end
end)
end)
``````

Caveats to take notes of:

• It is not scalable, if you decide to add more gifts to the list
• Limitation due to if statements
• Error prone due to `gifts["Gift" .. newValue]` can be nil, and indexing will cause an error(which however does not stop it from working)

It looks like you’re trying to build a system for the UI to change when a player has a number of gifts in their hands, and every 5 gifts will award them 1240 tokens? And then it resets to 0 gifts. Assuming that is what you want to do:

``````local Players = game:GetService("Players")

-- Refactored, right idea
local tokens = player:WaitForChild("Tokens")

for i = 1, 5 do
-- For scalable solution, change 5 with #GiftsFolder:GetChildren(),
-- assuming you move all of the pieces into a folder.
if newValue >= i then
else
end
end

if newValue >= 5 then
-- What if we got 6 gifts somehow?
-- For scalable solution, change the 5 to the same as above...
tokens.Value += 1240
end
end)
end)
``````

Yeah, almost worked! But thanks for your help, man.

Almost, if the player get only 5 gifts, they will get their reward.
gonna try rebuild the script from your help later, but thanks again, bud!

Gonna try this later, thanks for your help, too!