Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My minigame random map function isn't working?

Asked by 7 years ago
Edited 7 years ago

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)

0
Can you post the whole script please? I think something is wrong with one of your variables. starlebVerse 685 — 7y
0
Please post the whole script like CStarLam said or we can't help you. AstrealDev 728 — 7y
0
I've edited the post with the whole script. xJoshRyan 23 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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!

0
Hey thanks for the answer. Not sure if I misunderstood you (I did just copy and paste the script though after reading what you said) but I did this and now I get this... - Workspace.MainGame:15: attempt to call method 'GetChildren' (a nil value) xJoshRyan 23 — 7y
0
This script is getting the children twice. Remove :GetChildren() from the third line, and it should work joalars2 107 — 7y
0
I have updated my answer. Try again! :) starlebVerse 685 — 7y
0
Figured it not now, but I'll accept this answer :) xJoshRyan 23 — 7y
Ad

Answer this question