I’m making a tycoon game, and I have added a for loop that will loop through each individual instance in a folder, and in some cases, the folder will be empty. Instead of returning a nil
value, the engine completely skips over it. How can I fix that?
I don’t really get what you’re saying here. Can you maybe provide some snippets of your code?
I edited my post!
Sure, here it is!
for _, Following in (Button.Properties.Following:GetChildren()) do
local ButtonID = tonumber(Following.Name)
local ButtonObject = Buttons[tostring(ButtonID)]
local ButtonObjectPrevParent = ButtonObject.Parent
local ButtonGUID = HttpService:GenerateGUID(false)
ButtonObject.Name = ButtonGUID
ButtonObject.Parent = game.ReplicatedStorage.Tycoons.tmp
Button.Structure.Head.Touched:Connect(function(hit)
local Player: Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Date = DateTime.now():ToLocalTime()
if Tycoon:GetAttribute("Owner") == tostring(Player.UserId) and Data.Currencies.Cash >= Button.Properties.Cost.Value then
local dt = DateTime.now()
Data.Tycoon.Floors[Floor.Name].Buttons[Button.Name] = {
Time = {
["Date and time"] = {
Time = {
Second = dt:FormatUniversalTime("s", "en-us"),
Minute = dt:FormatUniversalTime("m", "en-us"),
Hour = dt:FormatUniversalTime("H", "en-us")
},
Date = {
["Day"] = dt:FormatUniversalTime("D", "en-us"),
Month = dt:FormatUniversalTime("M", "en-us"),
Year = dt:FormatUniversalTime("YYYY", "en-us")
}
},
},
ID = Button.Name
}
ButtonObject.Parent = ButtonObjectPrevParent
ButtonObject.Name = tostring(ButtonID)
FollowingRemaining -= 1
print(FollowingRemaining)
if FollowingRemaining <= 0 then
ObjectLink.Name = ObjectLinkPrevName
ObjectLink.Parent = ObjectLinkPrevParent
CurrencyModifier.CashUpdate(-Button.Properties.Cost.Value, Player.UserId)
Data.Tycoon.Value["Net Worth"] += Button.Properties.Cost.Value
Button:Destroy()
end
end
end)
end
I don’t think it will make a difference though
Correct me if I’m wrong, but what you’re saying is that the loop doesn’t run if the folder Button.Properties.Following
is empty? Since you are using a for loop and GetChildren()
, it’s not going to run because GetChildren()
returns a table of the children inside an instance and if the folder is empty it’s just going to return nothing and the for loop won’t run. If you really want to make a part of the code run no matter what, you can just put the code outside. (sorry if I misinterpreted your question)
EDIT: its won’t run not will run, sorry for that typo
You are correct.
I still want a variable of each individual “Following” Part
What are you trying to achieve here?
For loops are not the only type of loop in Roblox. You can use For, While, and Repeat. If you need at least 1 pass of code in your script, your loop should be a Repeat loop and not a For loop, since in Repeat your code will be executed at least once before being stopped.
So how can I implement it in my code?
Example: If you have 5 Children in the folder, but you want the code to run on maximum total children and minimum 1, then use this:
local RepeatCount = math.max(1, #Button.Properties.Following:GetChildren())
repeat
-- Code
RepeatCount -= 1
until RepeatCount == 0
Note: This could be used in a For Loop as well, but it would not be checking tables but rather a traditional counting Loop. However, I wanted to give an example of how to use Repeat.
I agree, repeat
is often forgotten (in my opinion) but honestly I think that generally for
loops are more straightforward, as for repeats, you have to implement a variable before it starts
A for loop through an empty table will not run the code in the for loop body.
for i,v in pairs({}) do
print'1'
end
in this example, it will never print 1.
To check if there are any children, you can do the following:
local children = Button.Properties.Following:GetChildren()
if #children > 0 then
-- there is at least 1 child
else
-- no children
end
EDIT : this works because GetChildren() returns an array. To check if anything is in a dictionary check next(my_table)~=nil