Afk script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

    I want this afk script to save the players which aren’t afk in a table

  2. What is the issue? Include screenshots / videos if possible!

    When printing the table out nothing shows

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

    I’ve tried to invert it, thinking maybe I switched the variables accidentally but that also gave no result.

Code for storing the players in a table. The _G.players global value is a table of players that have loaded and are ready to be teleported

local table = {}
local text = "text"
_G.AFK = {    --Ignore this I get an error if I don't initialize it
	text
}
while wait(0.5) do
	function afk()
		if #_G.player >= 2 then
		for i, plr in pairs(_G.players) do
			if not plr:FindFirstChild("isAFK") then     --If player has a "isAFK" bool value, don't store him, meaning he wont be teleported
				table.insert(plr)
			end
		end
		end
		return table
	end
	_G.AFK = afk()
end

Local script in which I fire the afk event:

local player = game.Players.LocalPlayer
local button = script.ScreenGui.TextButton
local remote = game.ReplicatedStorage.isAFK

button.MouseButton1Click:Connect(function()
	
	if not player:FindFirstChild("isAFK") then
		
		button.TextColor3 = Color3.fromRGB(25, 223, 25)
		remote:FireServer("AFK")
		
	else 
		button.TextColor3 = Color3.fromRGB(255,255,255)
		remote:FireServer("Not AFK")
		
	end
	
end)

Server script in which I assign the afk bool value that gets checked:

local remote = game.ReplicatedStorage.isAFK



remote.OnServerEvent:Connect(function(player, isAFK)
	
	if isAFK == "AFK" then
			
		local afkval = Instance.new("BoolValue")
		afkval.Name = "isAFK"
		afkval.Parent = player
			
	else
		
		player.isAFK:Destroy()
		
	end
	
end)

table.insert requires you to first define the table to put something into, then the value to put in.

Wouldn’t this mean the player will be put into the AFK table if they don’t have the AFK value?

Its quite strange and I don’t understand much of your code, but the main problem is definitely the first script. Easy fix:

local Players = game:GetService("Players")
local text = "text"
_G.AFK = {    --Ignore this I get an error if I don't initialize it
	text
}
while wait(0.5) do
	function afk()
		local afkTable = {} -- moved table down here so its always an empty one
		if #_G.player >= 2 then
			for i, plr in pairs(Players:GetPlayers()) do -- just go through all players since I dont know what _G.players even is and this seems more consistent
				if plr:FindFirstChild("isAFK") then -- remove the "not" from youir original script since we want to put the people in the afktable if they ARE afk... I think
					table.insert(afkTable, plr) -- insert player to table
				end
			end
		end
		return table
	end
	_G.AFK = afk()
end

Not sure if this works, but I hope it helps.

2 Likes

I will try it later as I don’t have much free time on my hands. Thank you for the answer and I will try it as soon as I can!
To answer your question about the player getting put into the AFK table when he is not afk, yes, I just named the table like that which I will change, but the table is used for getting the non afk players

1 Like

Then nvm me removing the not from the if statement.

Ok so your fix wouldn’t really work for my case since #_G.players is a table containing all the players who have loaded into the game or simply put “they passed the loading screen”.

Then you might want to remove this? This basically makes it so unless two players have loaded in already, nobody can be loaded into the table, right? That would mean nobody is ever loaded into the table.

Yes, they shouldn’t be loaded in the table, because the round can only start when 2 or more players are present
Edit: Since the program is an infinite loop this shouldn’t be a problem
Edit2: I will post a clip of the gameplay and what is happening

Edit: Here’s a short video of the problem. Sadly mega doesn’t support mkv in the browser video player so you will have to download the video

If you convert the video to an mp4 with a website you can also post it so that its imbeded on the devforum and I can watch it from here.

Edit: It might take me some time to reply since I have to do something

1 Like


I have also noticed a small mistake and now have this situation:
(The up is _G.players and the down is _G.AFK)
image

1 Like

Alright, so I don’t really see why you need a table containing the afk players if you have the value that tells you if they are afk or not in the player. When teleporting the players, you could just do:

if not plr:FindFirstChild("isAFK") then
...

That would make it much simpler. Also, I recommend setting an attribute on the player instead of using values, as far as I know, attributes are more performant than values. So you would do:

remote.OnServerEvent:Connect(function(player, isAFK)
	if isAFK == "AFK" then
		player:SetAttribute("isAFK", true)	
	else
        player:SetAttribute("isAFK", false)
	end
end)

And when you check if a player is AFK or not you’d just do:

if player:GetAttribute("isAFK") then
  -- Means they are afk
else
  -- Mean they are not afk
end

Which is a lot simpler

Ok so after goofing around a bit again I got an error on line 12 “attempted to call a nil value”
image

That’s because you are using .insert wrong. Here is the correct way to inser into tables:

table.insert(TableToInserTo, ThingToInserIntoTheTable)

table. is basically a library of functions to run on a table. By calling this library you can then access the function .insert()

1 Like

To answer your previous question, I need tables so I know which players to teleport into the round. My
round script basically works by getting the players from a table and teleport them. Teleporting them directly by checking if they have a value “isAFK” wont quite work for me because every player has to load, and “isAFK” isn’t there when they’re loading.

After doing what you said, I still get the error “attempt to call a nil value” at the plrs.insert(j, plr)

function afk()
		local j = 1
		if #_G.players >= 2 then
		wait(1)
		for i, plr in pairs(_G.players) do
			if not plr:FindFirstChild("isAFK") then     --If player has a "isAFK" bool value, don't store him, meaning he wont be teleported
				plrs.insert(j, plr)
				j = j + 1
			end
		end
		end
		return plrs

I printed out the player name before storing it into the table, meaning the player is very obviously there

Whoops, It seems I forgot how to use tables correctly and that was the whole problem. After fixing it, it works now, thank you all for your patience!

1 Like

Great to hear! Sorry I couldn’t reply in time, I had to do something, but its great that you managed to find the solution on your own!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.