Puts Player in Table, but Doesn't Teleport (AFK Timing Bug)

Hey, so I am testing with a Round-Based game. Everything works fine, except one of the players found a bug. If the player turns on AFK mode right after Intermission hits 0, then it adds the player into the _G.gameplayers table, but it doesn’t teleport them into the round (essentially giving that player a free win).

Here is the code:

while game.Players.NumPlayers < 2 do
	status.Value = "There Needs to be at least 2 Players to Begin"
	repeat wait(2) until game.Players.NumPlayers >= 2
end

for i = 15,0,-1 do
	status.Value = "Intermission... "..i
	wait(1)
end

_G.gameplayers = {}
for i, v in pairs(game.Players:GetPlayers()) do
	if v then
		if playerFolders[v.Name].isAFK.Value == false then
			table.insert(_G.gameplayers, v.Name)
		else
			print(v.Name.." is currently idle")
		end
	end
end

-- FIX AFK GLITCH - if player hits AFK in this moment, they are in table but not in round

local spleefArena = game.ServerStorage:WaitForChild("SpleefArena")

if #_G.gameplayers >= 2 then
	status.Value = "Teleporting Players to Arena"
	serverMode.Value = "Choosing"
	
	wait(1)
	
	local gameModes = game.ServerStorage:WaitForChild("Gamemodes"):GetChildren()
	local chosenMode = gameModes[math.random(1, #gameModes)]
	
	local SpleefSpawns = game.Workspace:WaitForChild("SpleefSpawns"):GetChildren()
	local LavaSpleefSpawns = game.Workspace:WaitForChild("LavaSpleefSpawns"):GetChildren()
	
	
	if chosenMode.Value == "Normal" then
		mode.Value = "Mode: Normal"
		serverMode.Value = "Normal"
		-- No real changes for Normal Mode
		-- Disable AFK Change Ability
		
		for i, v in pairs(game.Players:GetPlayers()) do
			if v then
				if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(v.UserId, 6905258) then
					game.Workspace[v.Name]:WaitForChild("Humanoid").WalkSpeed = 19
				else
					game.Workspace[v.Name]:WaitForChild("Humanoid").WalkSpeed = 16
				end
			end
		end
		
		for i, v in pairs(game.Players:GetPlayers()) do
			if v then
				if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(v.UserId, 6935643) then
					game.Workspace[v.Name]:WaitForChild("Humanoid").JumpPower = 35
				else
					game.Workspace[v.Name]:WaitForChild("Humanoid").JumpPower = 0
				end
			end
		end
		
		spleefArena:WaitForChild("Normal"):Clone().Parent = mapStorage
		for _, player in pairs(game.Players:GetPlayers()) do
			if player and #SpleefSpawns > 0 and playerFolders[player.Name].isAFK.Value == false then
				local playerRoot = player.Character:WaitForChild("HumanoidRootPart")
				local allSpleefSpawns = math.random(1, #SpleefSpawns)
				local randomSpawn = SpleefSpawns[allSpleefSpawns]
				if randomSpawn and playerRoot then
					game.ReplicatedStorage:WaitForChild("AFKRound"):FireClient(player, "InRound")
					table.remove(SpleefSpawns, allSpleefSpawns)
					playerRoot.Anchored = true
					playerRoot.CFrame = CFrame.new(randomSpawn.Position + Vector3.new(0, 1, 0))
					wait()
				end
			end
		end
		wait(2)
		for _, player in pairs(game.Players:GetPlayers()) do
			if player and playerFolders[player.Name].isAFK.Value == false then
				local playerRoot = player.Character:WaitForChild("HumanoidRootPart")
				wait()
				playerRoot.Anchored = false
			end
		end
		wait(3)
		for _, player in pairs(game.Players:GetPlayers()) do
			if player and playerFolders[player.Name].isAFK.Value == false then
				local playerRoot = player.Character:WaitForChild("HumanoidRootPart")
				wait()
				playerRoot.Anchored = false
			end
		end

Keep in mind, this is only a portion of the full script - this portion is where the bug is occurring, right after intermission and before teleportation.

If anyone has any idea how I could fix this bug please let me know.

I have tried inserting code into different sections of the script that check if the player’s AFK value is true and if it is then it updates the table, example of that here:

for _, player in pairs(game.Players:GetPlayers()) do
    if player then
        if playerFolders[player.Name].isAFK.Value == true then
            local Update = {}
            for _, Player in pairs(game.Players:GetPlayers()) do
                if Player then
                    for i, v in pairs(_G.gameplayers) do
                        if v ~= Player.Name then
                            table.insert(Update,1,v)
                        end
                    end
                end
            end
            _G.gameplayers = Update
        end
    end
end

Anyways, thanks for the help!

Are you trying to check and see if the player is AFK? if they are, do you want them to not teleport?

No, if a player turns on AFK mode right after intermission hits 0 before they get teleported, the player is added into the table for players in the round, but because their AFK is true, then they aren’t teleported into the match (essentially giving them a free win), I want to fix this, but I don’t know how.

I’ve tried adding some code into multiple places in the script before the round so it checks if a players AFK Value is true, and if so removing them from the in-round table, but nothing works.

Oh. i have something similar to this…
You Need to Use table.remove.

You Need to do something like this:

_G.gameplayers = {}
for i, v in pairs(game.Players:GetPlayers()) do
	if v then
		if playerFolders[v.Name].isAFK.Value == false then
			table.insert(_G.gameplayers, v.Name)
		else
             table.remove(_G.gameplayers, v.Name)
			print(v.Name.." is currently idle")
		end
	end
end

(this is for the intermission after 0 check) You Should Add a .Changed Event or a GetPropertyChangedSignal("Value") Line. After the Intermission if your having problems after the ‘0’ Happens.

2 Likes

Wow, I feel silly for not even thinking of this when it’s so simple… Thank you btw haha (I tested it and it does indeed work)

2 Likes

Great! I was Glad to Help. A Few Weeks Ago i Missed Something exactly like this. :heavy_check_mark: