So I'm making a minigame game, and I'm using a pickmap function to randomly pick a minigame. But for some reason it won't work. Here's the script:
local minPlayers = 2 local replicatedStorage = game:GetService("ReplicatedStorage") local maps = replicatedStorage:WaitForChild("Maps") function teleportAllPlayers() local target = CFrame.new(workspace.TeleportTarget.Position) for i, player in ipairs(game.Players:GetChildren()) do player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0) player.Playing.Value = 1 end end function pickGame() local mapList = maps:GetChildren() local selectedIndex = math.random(1,#maps) local map = mapList[selectedIndex]:Clone() map.Parent = game.Workspace map.Name = "CurrentMap" for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Minigame chosen! Teleporting players..." player.PlayerGui.GameStuff.ChosenGame.ImageLabel.Chosen.Text = workspace.CurrentMap.MapName.Value player.PlayerGui.GameStuff.ChosenGame:TweenPosition(UDim2.new(0.51, -250,0.5, -150), "Out", "Back", 3) wait(5) player.PlayerGui.GameStuff.ChosenGame:TweenPosition(UDim2.new(-1, -250,0.5, -150), "In", "Back", 3) end teleportAllPlayers() end function doMinigameStuff() end function unloadMap() if workspace:FindFirstChild("CurrentMap") then workspace.Map:Destroy() end for i, player in ipairs(game.Players:GetChildren()) do if player.Playing.Value == 1 then player.Character.Humanoid.Health = 0 end end end function playersCurrentlyPlaying() for i, player in ipairs(game.Players:GetChildren()) do if player.Playing.Value == 1 then return true end end end game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() player.Playing.Value = 0 end) end) local playing = Instance.new("IntValue", player) playing.Value = 0 playing.Name = "Playing" end) while(true) do wait(10) if #game.Players:getPlayers() >= minPlayers then if playersCurrentlyPlaying() then for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Waiting for current game to end..." end else for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Picking minigame..." end wait(4) pickGame() end else for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Need 2 or more players!" end end end
I should probably add that a lot of this is from the Wiki, I'm not too great at scripting.
It outputs the following:
Workspace.MainGame:15: attempt to get length of upvalue 'maps' (a userdata value)
I found the problem. In line 3, you only defined an instance, not a list of its children! If you want to pick a random item from a list, you need to index the table using :GetChildren()
first!
Here, let me show you:
local minPlayers = 2 local replicatedStorage = game:GetService("ReplicatedStorage") local maps = replicatedStorage:WaitForChild("Maps"):GetChildren() -- HERE!!! function teleportAllPlayers() local target = CFrame.new(workspace.TeleportTarget.Position) for i, player in ipairs(game.Players:GetChildren()) do player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0) player.Playing.Value = 1 end end function pickGame() math.randomseed(tick()) -- Optional. Make stuff more random! :) local selectedIndex = math.random(1,#maps) local map = maps[selectedIndex]:Clone() map.Parent = game.Workspace map.Name = "CurrentMap" for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Minigame chosen! Teleporting players..." player.PlayerGui.GameStuff.ChosenGame.ImageLabel.Chosen.Text = workspace.CurrentMap.MapName.Value player.PlayerGui.GameStuff.ChosenGame:TweenPosition(UDim2.new(0.51, -250,0.5, -150), "Out", "Back", 3) wait(5) player.PlayerGui.GameStuff.ChosenGame:TweenPosition(UDim2.new(-1, -250,0.5, -150), "In", "Back", 3) end teleportAllPlayers() end function doMinigameStuff() end function unloadMap() if workspace:FindFirstChild("CurrentMap") then workspace.Map:Destroy() end for i, player in ipairs(game.Players:GetChildren()) do if player.Playing.Value == 1 then player.Character.Humanoid.Health = 0 end end end function playersCurrentlyPlaying() for i, player in ipairs(game.Players:GetChildren()) do if player.Playing.Value == 1 then return true end end end game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() player.Playing.Value = 0 end) end) local playing = Instance.new("IntValue", player) playing.Value = 0 playing.Name = "Playing" end) while(true) do wait(10) if #game.Players:getPlayers() >= minPlayers then if playersCurrentlyPlaying() then for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Waiting for current game to end..." end else for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Picking minigame..." end wait(4) pickGame() end else for i, player in ipairs(game.Players:GetChildren()) do player.PlayerGui.GameStuff.TopText.TextLabel.Text = "Need 2 or more players!" end end end
Remember: Using :WaitForChild()
only returns the instance itself, not the children, so try using :GetChildren()
the next time you are trying to get the list of children from a folder and so on.
If you have a question. please leave a comment below. Thanks!