You can write your topic however you want, but you need to answer these questions:
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
What is the issue? Include screenshots / videos if possible!
When printing the table out nothing shows
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
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
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
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
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