Hi, I’m currently making a game that involves idling for some time, but Roblox’s anti-afk system is messing things up. I came up with this solution:
local rs = game:GetService("ReplicatedStorage")
local plrs = game:GetService("Players")
local ts = game:GetService("TeleportService")
local ws = game:GetService("Workspace")
local afkZone = ws.AFKZONE
rs.Remotes.AFKTrigger.OnServerEvent:Connect(function(player: player)
local data = {
autoRejoin = true
}
local options = Instance.new("TeleportOptions")
options.ServerInstanceId = game.JobId
options:SetTeleportData(data)
ts:Teleport(game.PlaceId, player, options)
end)
plrs.PlayerAdded:Connect(function(player: Player)
local data = player:GetJoinData()
if data.autoRejoin then
local char = player.Character or player.CharacterAdded:Wait()
task.wait(2)
char:PivotTo(afkZone.CFrame)
end
end)
The “AFKTrigger” remote event is called using Player.Idled event every 19 minutes.
However, if there’s only one player in the server, the server shuts down and player is unable to re-connect automatically.
You can only make the Player Rejoin the game by sending him to Places.
if hes idle then hes idle, nothing to do about it.
maybe save TeleportData of player what hes doing and thats it.
local players = game:GetService('Players')
local teleportService = game:GetService('TeleportService')
local textChatService = game:GetService('TextChatService')
local commands = textChatService:WaitForChild('TextChatCommands')
local rejoinCommand = Instance.new('TextChatCommand')
rejoinCommand.PrimaryAlias = '/rejoin'
rejoinCommand.Parent = commands
rejoinCommand.Triggered:Connect(function(source, text)
local userId = source.UserId
local player = players:GetPlayerByUserId(userId)
if not player then
return
end
local playersCount = #players:GetPlayers()
local teleportOptions
if playersCount > 1 then --[[ we check for the number of players to be greater than 1
because when we try joining this server, it will have
already been marked as shutting down (we will encounter
an inf. load), so we need to teleport them to a new server,
thus we want to not pass a TeleportOptions so we get the default
teleporting behaviour.
don't believe there's a way to get around the server shutdown behaviour ]]
teleportOptions = Instance.new('TeleportOptions')
teleportOptions.ServerInstanceId = game.JobId
end
teleportService:TeleportAsync(game.PlaceId, { player }, teleportOptions)
end)
I’m going to be honest, even if you do find a way to bypass this, the chances are that roblox will patch it sooner or later. Roblox doesn’t want you to afk for long periods of time because it costs them a ton of money to maintain the servers, so they of course cut any unnecessary connection. I would rather look into the design of your game for a way so you don’t have to deal with this issue at all, because this just look like a game design flaw to me.