Can't make an AFK button to my game

I made a minigame system and I wanted to add an afk button (However it didn’t worked). I putted a script like this in the button:
local player = game.Players.LocalPlayer --Gets the player
script.Parent.MouseButton1Click:connect(function()
if player.AFK.Value == true then --Looks for the AFK value in the player
player.AFK.Value = false --Changes the value
script.Parent.BackgroundColor3 = Color3.fromRGB(255, 0, 0) --Changes the color of the button
else
player.AFK.Value = true–Changes the value
script.Parent.BackgroundColor3 = Color3.fromRGB(0, 255, 0)–Changes the color of the button
end
end)

I also added this code on the status bar (NOT IN THE SAME SCRIPT):
if afk == true then – If the player enabled afk
if workspace.Main.Stat.Value == “Race starting!” then --If the race is starting
oldpos = pos.HumanoidRootPart.Position --Gets the old position of the player
wait(2)–waits for the game to start
character.HumanoidRootPart.CFrame = CFrame.new(oldpos) --Teleports to old position
end

end

(The script is in a “while wait() do” loop)

What is the problem with my scripts?
Maybe I should use a while true do loop instead of while wait?

try defining the Afk at the begin:

local AFK = false

@EHGTR1903 I have already done this.
local afk = game.Players.LocalPlayer.AFK.Value

I don’t see it in the code. Where is it?

It still doesn’t work :frowning:

@EHGTR1903 Its at the beginning. I don’t want to show it but it is there. Other variables like oldpos is also defined

Where is this script?
(30 char)

@EHGTR1903 It is in the same script. I am going to show it:
local pos = game.Players.LocalPlayer.Character
local afk = game.Players.LocalPlayer.AFK.Value
local oldpos = pos.HumanoidRootPart.Position

local player = game.Players.LocalPlayer

while wait ( ) do
script.Parent.Text = workspace.Main.Stat.Value
if workspace.Main.Stat.Value == “1 seconds left.” then
character = game.Players.LocalPlayer.Character
character.HumanoidRootPart.CFrame = CFrame.new(workspace.spawnbrick.Position)
end
if afk == true then – If the player enabled afk
if workspace.Main.Stat.Value == “Race starting!” then --If the race is starting
oldpos = pos.HumanoidRootPart.Position --Gets the old position of the player
wait(2)–waits for the game to start
character.HumanoidRootPart.CFrame = CFrame.new(oldpos) --Teleports to old position
end

end

end

That’s not what I meant. I meant that where is the script located is it in workspace or in startergui, where?

@EHGTR1903 starter gui
(text for 30 character)

Are you defining the AFK variable client and reading from server?

where is the value? if it’s in starter player it will dissapear

@ImSinfullyFrosty what do you mean?
(Sorry I had to wait 2 minutes)

From what i see your writing everything in a local script (client) so thats not the problem. Though you need to take out the click detection inside the loop because you are creating instances (A MouseButton1Click is labeled in an instance and will fire when ever someone clicks mouse button. Each time you create a mousebutyon1click you create an instance so evertime you lopp through that code your creating a new instance and it will cause extreme lag after a while.

There is no click detection on the script. Its on another script.

Can someone help???

I would believe you’d just have as per usual:

Main Minigame Script : Server Script
Afk Button Script : Local Script
Remote Event

Essentially:
Afk Button calls the remote event on the client of course to the server.

Server gets remote event call with an argument that always returns, which is of course the player that called it.

Server shoves them into a table to ignore, and for the players currently player are kept in another table for playing:

Local Script

RemoteEvent.FireServer(true) -- true for afk

Server Script:

local PlayingTable = {} -- you could probs go without it :P
local AfkTable = {}

RemoteEvent.OnServerEvent:Connect(function(Player, IsAfk)
    if IsAfk then
        table.insert(AfkTable, Player) -- Shoves them into table to be "afk"
    else
        AfkTable[Player] = nil
        table.insert(PlayingTable, Player)
    end
end)

-- Main Part of Minigame
while true do
    if PlayingTable == "bla bla bla" then
    --continue
    end
end

Thats a really rough example of how it would be done. This works perfectly fine with any minigame system and if you really wanted to, you can make this into a sort of module for future minigames, games.

Sources to help:

1 Like

I will test it tomorrow. If it will work, thanks