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

I'm making an AFK button, but if there are 2 players and if one is afk, the other player will win?

Basically, I've been making a PVP game with rounds. I made an AFK button, everything works fine, but if there are only 2 players and one of them is AFK, the game still starts but the AFK player doesn't join, so the other player automatically wins. Help would be appreciated

(There are 2 values that are inside the player, either "NotPlaying" or "Playing")

Here is the script:

-- Define Variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 120

local Reward = 35

-- Game Loop

while true do
	
	Status.Value = "Waiting for enough players"
	
	repeat wait(1) until game.Players.NumPlayers >= 2
	
	Status.Value = "Intermission"
	
	wait(20)

	local AvailableMaps = MapsFolder:GetChildren()

	local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]


	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace
	
	Status.Value = "Picking a map..."
	
	local plrs = {}
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player:FindFirstChild("Playing") and player then
			table.insert(plrs,player)
		end
	end
	
	wait(2)

(This is only the part of the script where it adds the players)

1 comment

0

Can’t you simply check the amount of non afk players and if it is less than 2, then the game wont start

1 answer

✓ Accepted answer

Your Issue is simple enough to fix. When the round is ready to begin, simply check if there are 2 or more players with Playing true and NotPlaying false. If there are 2 or more players with Playing true and NotPlaying false, then let the round begin. If not, then, like you did at the beginning of the round, wait until there are enough players with AFK Mode Off.

7 comments

0

Oh wait, it's not working.. What I did was repeat wait(1) until game.Players.NumPlayers >= 2 and game.Players:FindFirstChild("Playing") >= 2. What did I do wrong?

0

What it does is that it stays on the "Waiting for enough players" status even though both players have the value "Playing" inside their player

0

Is it giving you an error?

0

Oh wait, I see the error: you need to do this instead: repeat wait(1) until game.Players.NumPlayers >= 2 and game.Players:FindFirstChild("Playing").Value >= 2

0

You didn't put .Value after finding Playing

0

Oh, thank you.

0

No Problem!

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