Player Idled Function issues

I’m not too familiar with the Idled function. However I’m running into the issue of getting it to fire. The intentions is for it to kicks players if they have been idled for 5 minutes. Maybe I am setting it up wrong but this is running in a server script

game:GetService("Players").PlayerAdded:Connect(function(AFK)
	AFK.Idled:Connect(function(Time)
		print("Time "..time())
		if Time >= 300 then
			game.Players.LocalPlayer:Kick("You have been AFK for 5 Minutes.")
		end
	end)
end)
1 Like

Sorry for the delayed response. It takes a long time to write a detailed reply lol.

I replicated your code and have run into the same issue. After some research, I have found out the the player.Idled event only fires on the client for some reason.

I tested it out on the client as well and it seems to work, however, it is not exactly secure. When things that should be handled on the server are handled on the client, information can be altered and is generally not a good idea. I will keep thinking on ways around this.

Here is the altered code for the client script:

-- Idle Script (Client)

-- Constants
local KICK_MESSAGE = "You have been AFK for 5 Minutes."
local AFK_TIME = 300 -- In seconds

-- Variables
local players = game:GetService("Players")

-- Functions
function Kick_Player(player: Player)
	player:Kick(KICK_MESSAGE)
end

function Wait_For_Idle(player: Player)
	player.Idled:Connect(function(idle_time)
		print("Player has entered the idle state")
		
		if idle_time >= AFK_TIME then
			Kick_Player(player)
		end
	end)
end

-- Initialize
players.PlayerAdded:Connect(Wait_For_Idle)

A thought I had was to detect the player.Idled event on the client and then fire the server through a RemoteEvent that would then count down from the time the server was fired, which is the most secure solution I have thought about so far.

Here is the ServerScript (ServerScriptService) for the proposed solution:

-- Idle Script (Server Solution)

-- Constants
local KICK_MESSAGE = "You have been AFK for 5 Minutes."
local AFK_TIME = 300 -- In seconds

-- Variables
local players = game:GetService("Players")

local replicated_storage = game:GetService("ReplicatedStorage")
local idle_event = replicated_storage.Idle

local player_counts = {}

-- Functions
function Kick_Player(player: Player)
	player:Kick(KICK_MESSAGE)
end

function Count_Down(player: Player)
	print("Player is idle")
	
	if player_counts[player.Name] ~= nil then
		if (os.time() - player_counts[player.Name]) >= AFK_TIME then
			Kick_Player(player)
		end
	end
	
	player_counts[player.Name] = os.time()
end

-- Initialize
idle_event.OnServerEvent:Connect(Count_Down)

And then the LocalScript (StarterPlayerScripts):

-- Idle Script (Client Solution)

-- Variables
local players = game:GetService("Players")
local player = players.LocalPlayer

local replicated_storage = game:GetService("ReplicatedStorage")
local idle_event = replicated_storage.Idle

-- Functions
function Wait_For_Idle()
	player.Idled:Connect(function(idle_time)
		print("Player is idle")
		idle_event:FireServer()
	end)	
end

-- Initialize
Wait_For_Idle()
1 Like

Thank you very much for the help. I will give this solution a try!

1 Like

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