So I already had a problem with this script that I posted about yesterday. My problem now is that in an in pairs loop, it’s getting the children of an object, but it’s printing both children, for some weird reason. Basically, I’m making a minigames game, where it randomly selects a minigame to play. I’ve included my script and a link to the video of the gameplay down below.
local Players, ReplicatedStorage = game.Players,game.ReplicatedStorage
local player, playerGui, MainGui = Players.LocalPlayer, Players.LocalPlayer:WaitForChild("PlayerGui"), Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("MainGui")
local changeTextEvent = ReplicatedStorage:WaitForChild("ChangeText")
changeTextEvent.OnClientEvent:Connect(function(timeUntilNextGame)
print("Updated")
MainGui.StatusLabel.Text = "Time Until Next Round: " ..timeUntilNextGame
local TweenService = game:GetService("TweenService")
local goal = {}
goal.Position = UDim2.new(0.5,0,.5,0)
local info = TweenInfo.new(1)
local Tween = TweenService:Create(MainGui.RoundDisplayFrame,info,goal)
Tween:Play()
local stages = game.Workspace.Stages:GetChildren()
local stageName
local stageSelected
while timeUntilNextGame >=1 do
stageName = stages[math.random(1,#stages)].Name
MainGui.RoundDisplayFrame.Display.Text = stageName
wait(.5)
end
for _, stage in pairs(game.Workspace.Stages:GetChildren()) do
print(stage)
print(stageName)
if stage.Name == stageName then
print("Same")
stageSelected = stage
end
end
local spawns = {}
for _, spawnPoint in pairs (stageSelected:GetChildren()) do
if spawnPoint.Name == "Spawn" then
table.insert(spawns,spawnPoint,#spawns+1)
end
end
game.ReplicatedStorage.SpawnPlayers:FireServer(spawns)
local leaveGoal = {}
leaveGoal.Position= UDim2.new(.5,0,1.5,0)
local leaveInfo = TweenInfo.new(1)
local leaveTween = TweenService:Create(MainGui.RoundDisplayFrame,leaveInfo,leaveGoal):Play()
end)
When the loop is done going through the code for the first Instance, it’ll start back at the beginning using the second one. This means that the names of any Instances inside the first layer of the folder will be printed because the print(stage) is not included inside of any conditional statements that would prevent it from doing so.
If you added it inside of the if stage.Name == stageName then statement, it would only print that value if it matched.
if stage.Name == stageName then
print(stage)
print("Same")
stageSelected = stage
end
I don’t every remember for loops working like that. I expected the result to be two different prints, because it was printing once for each item. Correct me if I’m wrong.
The point was they are supposed to be the same, but they aren’t, because, I’m assuming, that selection doesn’t have a name property that isn’t nil
local ReplicatedStorage = game:GetService("ReplicatedStorage")
for Index, Item in ipairs(ReplicatedStorage:GetChildren()) do
warn("Loop #"..Index.." - "..Item.Name)
--[[ Each time this function loops, the index/amount of times it has looped
will appear, alongside the name of the item in that index --]]
if Item:IsA("BasePart") and Item.Name == "hi" then -- If the item in the current iteration is a BasePart and has a name of "Test", then...
print("Nice!") -- "Nice!" will be printed into the Output because the condition was met
end
end
--[[ Using the example image above this codeblock, here's what would appear in the Output:
Loop #1 - Folder - Server
Loop #2 - Accessory - Server
Loop #3 - RemoteFunction - Server
Loop #4 - ModuleScript - Server
Loop #5 - Part - Server
Loop #6 - hi - Server
Nice! - Server
Loop #7 - ScreenGui - Server
--]]
The example Output demonstrated that the name of every item on the first layer of the ReplicatedStorage was printed, while “Nice!” was only printed during the iteration where the Instance called “hi” was found at (which was one of the requirements for the conditional statement to be met).
Other Responses:
Even if the Instance does not have a name (if it was manually set to be blank by a script), checking if its name is equal to itself will return true because "" is equal to "".
I presumed that you were referring to the variable that you created called “stageName” which stored the name of the chosen stage:
This means that when you call print(stage), that’ll print the name of the stage on the current iteration of the loop (because of the word used to refer to the “value” in the loop) and calling print(stageName) will print the value stored inside of the “stageName” variable.
Hey I’m sorry I made you write this out lol. I figured it out already. It was just that the while loop part was never being run, and it didn’t need to be there, so I got rid of it.