Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Detecting Players wont work?

I am trying to detect players so when there are more than two, a gui will come up with some information of a map and then take them to the map. If there is less than 1 player, a hint will come up saying you need more than 1 player. Though when I start a server with two people, the hint is still up.

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

2 answers

Okay, so you're code is kinda inefficient, but I'll write mine in the same manner so that you can understand it. The problem here is, you are creating a hint every time the loop runs. This means there will end up being thousands of hints, and you never remove them from the Workspace. My solution would be to use :findFirstChild() and :Destroy().

I will mark the areas I have changed:

while wait(.5) do
    if game.Players.NumPlayers >= 2 then
	if game.Workspace:findFirstChild("Hint") then -- If there happens to be a hint
		game.Workspace.Hint:Destroy() -- Destroy the hint
	end
        ranmap = math.random(1, #maps)
        wait(1)
        chosen = maps[ranmap]
        chosenclone = chosen:Clone()
        chosenclone.Parent = game.Workspace
        if chosenclone then
            game.StarterGui.ScreenGui.Frame.Visible = true
            game.StarterGui.ScreenGui.Frame.Map.Visible = true
            game.StarterGui.ScreenGui.Frame.Madeby.Visible = true
            game.StarterGui.ScreenGui.Frame.MapName.Visible = true
        end
        wait(5)
        spawn = chosenclone.SpawnPoint
        wait(5)
        for i,v in pairs(game.Players:GetPlayers()) do
            name = v.Name
            check = game.Players.Player.Character
            if check then
                checkHumanoid = check:FindFirstChild("Humanoid")
                if checkHumanoid then
                    check:MoveTo(spawn.Position)
                end
            end
        end
    else
	if not(game.Workspace:findFirstChild("Hint")) then  -- If a hint is not a child of the workspace
        	h = Instance.new("Hint", game.Workspace)
        	h.Text = "You need more than 1 player to play."
	end
    end
end

:FindFirstChild

:Destroy

while true do
    if game.Players.NumPlayers == 2 or game.Players.NumPlayers >2 then
        ranmap = math.random(1, #maps)
        wait(1)
        chosen = maps[ranmap]
        chosenclone = chosen:Clone()
        chosenclone.Parent = game.Workspace
        if chosenclone then
            game.StarterGui.ScreenGui.Frame.Visible = true
            game.StarterGui.ScreenGui.Frame.Map.Visible = true
            game.StarterGui.ScreenGui.Frame.Madeby.Visible = true
            game.StarterGui.ScreenGui.Frame.MapName.Visible = true
        end
        wait(5)
        spawn = chosenclone.SpawnPoint
        wait(5)
        for i,v in pairs(game.Players:GetPlayers()) do
            name = v.Name
            check = game.Players.Player.Character
            if check then
                checkHumanoid = check:FindFirstChild("Humanoid")
                if checkHumanoid then
                    check.Torso.CFrame = spawn.CFrame
                end
            end
        end
    else
        h = Instance.new("Hint", game.Workspace)
        h.Text = "You need more than 1 player to play."
    end
wait(.5)
end

3 comments

0

Changing it to Greater than 1 doesn't help. It is pretty much the same thing as what I put.

0

Try again

0

That won't help either. I already tried it. ">=" means greater than or equal to. ">" means greater than. They are almost the same. You made it have to be greater than one. I made it greater than or equal to 2. So it has to be above one.

This discussion is preserved as it was. New answers, comments, and votes are closed.