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
game.Players.PlayerAdded:Connect(function(player)
if player.Gifts.Value == 1 then
gifts.Gift1.ImageColor3 = Color3.new(1, 1, 1)
else
gifts.Gift1.ImageColor3 = Color3.new(0, 0, 0)
end
if player.Gifts.Value == 2 then
gifts.Gift2.ImageColor3 = Color3.new(1, 1, 1)
else
gifts.Gift2.ImageColor3 = Color3.new(0, 0, 0)
end
if player.Gifts.Value == 3 then
gifts.Gift3.ImageColor3 = Color3.new(1, 1, 1)
else
gifts.Gift3.ImageColor3 = Color3.new(0, 0, 0)
end
if player.Gifts.Value == 4 then
gifts.Gift4.ImageColor3 = Color3.new(1, 1, 1)
else
gifts.Gift4.ImageColor3 = Color3.new(0, 0, 0)
end
if player.Gifts.Value == 5 then
gifts.Gift5.ImageColor3 = Color3.new(1, 1, 1)
print("hooray!")
player.Tokens.Value = player.Tokens.Value + 1240
else
gifts.Gift5.ImageColor3 = Color3.new(0, 0, 0)
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
}
--Changing each gift value
local giftValue = player.Gifts.Value
for x(index), gift(Gift(1-x) in ipairs(gifts:GetChildren()) do -- :GetChildren() returns an array
giftValue == gift.GiftNumber.Value then
gift.ImageColor3 = giftTable[x] -- this will set the color3 to the associated color3 in the array at index x.
else
gift.ImageColor3 = Color3.new(0,0,0)
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?
Players.PlayerAdded:Connect(function(player)
local gifts = player:WaitForChild("Gifts")
local tokens = player:WaitForChild("Tokens")
gifts.Changed:Connect(function(newValue)
if newValue > 0 and newValue <= 5 then
gifts["Gift" .. newValue].ImageColor3 = Color3.new(1, 1, 1)
if newValue == 5 then
tokens.Value += 1240
end
else
gifts["Gift" .. newValue].ImageColor3 = Color3.new(0, 0, 0)
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
Players.PlayerAdded:Connect(function(player)
local gifts = player:WaitForChild("Gifts")
local tokens = player:WaitForChild("Tokens")
gifts.Changed:Connect(function(newValue)
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
gifts["Gift" .. i].ImageColor3 = Color3.new(1,1,1)
else
gifts["Gift" .. i].ImageColor3 = Color3.new(0,0,0)
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!