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

Detecting players won't work right?

Asked by
Spoookd 32
8 years ago

I am trying to do something with a gui if there are 2 or more players in game. That stuff is under the if statement. If there is only 1 player in the game, a hint is supposed to come up saying you need more than 1 player. Though when I start a server with 2 players, on the first player's screen the hint is up, but the second player's screen, the gui is up but nothing is happening.

maps = game.ServerStorage:GetChildren()
local player = game:GetService"Players".LocalPlayer
local guis = player:WaitForChild"PlayerGui"

    if game.Players.NumPlayers >= 2 then
        guis.ScreenGui.Frame.Map.Visible = false
        guis.ScreenGui.Frame.Madeby.Visible = false
        wait(3)
        ranmap = math.random(1, #maps)
        guis.ScreenGui.Frame.Visible = true
        guis.ScreenGui.Frame.Beingchosen.Visible = true
        wait(1)
        chosen = maps[ranmap]
        chosenclone = chosen:Clone()
        chosenclone.Parent = game.Workspace
        guis.ScreenGui.Frame.Map.Visible = true
        guis.ScreenGui.Frame.Madeby.Visible = true
        guis.ScreenGui.Frame.Madeby.Text = "Map is " .. chosen.Name .. " By: Spoookd"
        wait(5)     
        spawn = chosenclone.SpawnPoint
        for i,v in pairs(game.Players:GetPlayers()) do
        name = v.Name
        check = game.Workspace:FindFirstChild(name)
        if check then
            checkHumanoid = check:FindFirstChild("Humanoid")
            if checkHumanoid then
                check:MoveTo(spawn.Position)
            end
        end
        guis.ScreenGui.Frame.Visible = false
        end
    end

guis.ScreenGui.Frame.Visible = false
h = Instance.new("Hint", game.Workspace)
h.Text = "You need more than 1 player to play."
0
NOT 100% sure, but I think it's a loading issue. Try adding a wait for the player (not the character) gaberdell 71 — 8y
0
So where should I put the wait? Spoookd 32 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

Use a loop to check if NumPlayers is greater than 2, instead of checking once

Just place the check in a while loop. Like so,

maps = game.ServerStorage:GetChildren()
local player = game:GetService"Players".LocalPlayer
local guis = player:WaitForChild"PlayerGui"


while wait(.5) do
    if game.Players.NumPlayers >= 2 then
        guis.ScreenGui.Frame.Map.Visible = false
        guis.ScreenGui.Frame.Madeby.Visible = false
        wait(3)
        ranmap = math.random(1, #maps)
        guis.ScreenGui.Frame.Visible = true
        guis.ScreenGui.Frame.Beingchosen.Visible = true
        wait(1)
        chosen = maps[ranmap]
        chosenclone = chosen:Clone()
        chosenclone.Parent = game.Workspace
        guis.ScreenGui.Frame.Map.Visible = true
        guis.ScreenGui.Frame.Madeby.Visible = true
        guis.ScreenGui.Frame.Madeby.Text = "Map is " .. chosen.Name .. " By: Spoookd"
        wait(5)     
        spawn = chosenclone.SpawnPoint
        for i,v in pairs(game.Players:GetPlayers()) do
            name = v.Name
            check = game.Workspace:FindFirstChild(name)
            if check then
                checkHumanoid = check:FindFirstChild("Humanoid")
                if checkHumanoid then
                    check:MoveTo(spawn.Position)
                end
            end
            guis.ScreenGui.Frame.Visible = false
        end
    else
        guis.ScreenGui.Frame.Visible = false--I moved this
        h = Instance.new("Hint", game.Workspace)
        h.Text = "You need more than 1 player to play."
    end
end

This might not work but that was one of your problems.

If F is E then local scripts can't accessServerStorage. This is just some useful info.

Good Luck!

Ad

Answer this question