Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I add an AFK checker to this?

Asked by 4 years ago

I just started working on adding an AFK button to my game's settings and was wondering how I would rewrite my current code to check if the player is AFK and only affect players that are not AFK

local seconds = game.Workspace.Time
local title = game.Workspace.Title
local ingame = game.Workspace.InGame.Value
local players = game.Workspace.PlayersInRound
local event = game.ReplicatedStorage.Events.ChosenMap

local Map

while true do
    wait(1)
    seconds.Value = seconds.Value - 1

    if seconds.Value == 0 and ingame == 0 then
        local chosenMap = math.random(1,5)

        Map = game.ReplicatedStorage.Maps:FindFirstChild("Map"..chosenMap):Clone()
        Map.Parent = game.Workspace

        ingame = 2
        title.Value = "TELEPORTING PLAYERS IN"
        seconds.Value = 10

        game.Workspace.Lobby.NSpawner.Enabled = false

        local teleport = game.Players:GetChildren()
            for i = 1, #teleport do
            teleport[i]:LoadCharacter()
            teleport[i].Character.Humanoid.WalkSpeed = 0
        end

        event:FireAllClients(chosenMap)
    end

    if seconds.Value == 0 and ingame == 1 then
        ingame = 0
        title.Value = "INTERMISSION"
        seconds.Value = 30

        local teleport = game.Players:GetChildren()
        for i = 1, #teleport do
            teleport[i]:LoadCharacter()
        end

        Map:Destroy()
    elseif players.Value <= 0 and ingame == 1 then
        ingame = 0
        title.Value = "INTERMISSION"
        seconds.Value = 30

        local teleport = game.Players:GetChildren()
        for i = 1, #teleport do
            teleport[i]:LoadCharacter()
        end

        Map:Destroy()
    end

    if seconds.Value == 0 and ingame == 2 then
        ingame = 1
        title.Value = "ROUND ENDS IN"
        seconds.Value = 60

        game.Workspace.Lobby.NSpawner.Enabled = true

        local teleport = game.Players:GetChildren()
            for i = 1, #teleport do
            teleport[i].Character:MoveTo(Vector3.new(-1000, 3, 0))
            teleport[i].Character.Humanoid.WalkSpeed = 16
            players.Value = #teleport
        end
    end
end
0
Sorry if this is something easy I'm still learning how to script. JJBIoxxer 50 — 4y

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago

First you need to create a ScreenGui in starterGui (if you don't already have one). Then add a TextButton to that ScreenGui. Following LocalScript (NOT script) you should Parent to that button.

game:GetService("ReplicatedStorage")
local AfkEvent = ReplicatedStorage:WaitForChild("AFKEvent")
local button = script.Parent
local afk = false
button.Text = "Playing"

button.MouseButton1Click:Connect(function()
    if afk then 
        afk = false
        button.Text = "AFK"
        AfkEvent:Fire(false)
    else -- false
        afk = true
        button.Text = "Playing"
        AfkEvent:Fire(true)
    end
end)

Then you need to adjust your script a little bit.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AfkEvent = Instance.new("RemoteEvent")
AfkEvent.Name = "AfkEvent"
AfkEvent.Parent = ReplicatedStorage

local seconds = game.Workspace.Time
local title = game.Workspace.Title
local ingame = game.Workspace.InGame.Value
local players = game.Workspace.PlayersInRound
local event = game.ReplicatedStorage.Events.ChosenMap

local afkPlayers = instance.new("Folder")
afkPlayers.Name = "AFKPlayers"
afkPlayers.Parent = workspace

AfkEvent.OnServerEvent:Connect(function(player,afkStatus)
    if afkStatus = false
        player.Parent = afkPlayers
    else
        player.Parent = players
    end
end)

local Map

while true do
    wait(1)
    seconds.Value = seconds.Value - 1

    if seconds.Value == 0 and ingame == 0 then
        local chosenMap = math.random(1,5)

        Map = game.ReplicatedStorage.Maps:FindFirstChild("Map"..chosenMap):Clone()
        Map.Parent = game.Workspace

        ingame = 2
        title.Value = "TELEPORTING PLAYERS IN"
        seconds.Value = 10

        game.Workspace.Lobby.NSpawner.Enabled = false

        local teleport = game.Players:GetChildren()
            for i = 1, #teleport do
            teleport[i]:LoadCharacter()
            teleport[i].Character.Humanoid.WalkSpeed = 0
        end

        event:FireAllClients(chosenMap)
    end

    if seconds.Value == 0 and ingame == 1 then
        ingame = 0
        title.Value = "INTERMISSION"
        seconds.Value = 30

        local teleport = game.Players:GetChildren()
        for i = 1, #teleport do
            teleport[i]:LoadCharacter()
        end

        Map:Destroy()
    elseif players.Value <= 0 and ingame == 1 then
        ingame = 0
        title.Value = "INTERMISSION"
        seconds.Value = 30

        local teleport = game.Players:GetChildren()
        for i = 1, #teleport do
            teleport[i]:LoadCharacter()
        end

        Map:Destroy()
    end

    if seconds.Value == 0 and ingame == 2 then
        ingame = 1
        title.Value = "ROUND ENDS IN"
        seconds.Value = 60

        game.Workspace.Lobby.NSpawner.Enabled = true

        local teleport = game.Players:GetChildren()
            for i = 1, #teleport do
            teleport[i].Character:MoveTo(Vector3.new(-1000, 3, 0))
            teleport[i].Character.Humanoid.WalkSpeed = 16
            players.Value = #teleport
        end
    end
end

If the buton will be pressed during game, the player will not be teleported back. I leave it to you to find a solution to that problem :)

Ad

Answer this question