Need help with this AFK Tag script

Hello. So, I want this AFK text to appear like this “Username (AFK)” when the player is AFK. But the problem here is that when a player goes AFK the AFK will appear to every single player in the game and making their username the same including the AFK text. Here is the script I did.

Here is a normal script that is located at the ServerScriptService.

--/ Variables

local ServerStorage = game:GetService("ServerStorage")

local Overhead = ServerStorage:WaitForChild("Overhead")

local TweenService = game:GetService("TweenService")

local groupId = "8402934"
local minRank = 125

--/ Functions

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)

		local clone = Overhead:Clone()
		local groupRank = plr:GetRoleInGroup(groupId)
		clone.Parent = character.Head
		clone.Label.Text = plr.Name
		clone.Rank.Text = groupRank
		
		if plr:GetRankInGroup(groupId) >= minRank then
			clone.Icon.Visible = true
		else
			clone.Icon.Visible = false
		end
		if plr:GetRankInGroup(groupId) == 0 then
			clone.Rank.Text = "Customer"
		end
		
		local ReplicatedStorage = game:GetService("ReplicatedStorage")

		local AfkEvent = Instance.new("RemoteEvent")
		AfkEvent.Name = "AFKEvent"
		AfkEvent.Parent = ReplicatedStorage

		local function setAfk(player, afk)
			if afk then
				clone.Label.Text = player.Name.." (AFK)"
			else
				clone.Label.Text = player.Name
			end
		end

		AfkEvent.OnServerEvent:Connect(setAfk)
		
		
		character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None

		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId,14217559) then
			clone.Parent = character.Head
			clone.Label.Text = plr.Name
			while true do
				local Color = Color3.new(math.random(), math.random(), math.random())
				local ColorTween = TweenService:Create(clone.Label, TweenInfo.new(3), {TextColor3 = Color})
				ColorTween:Play()
				wait(3)

			end
		end

	end)
end)

And here is the Local Script that is located at StarterPlayer > StarterPlayerScripts

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

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AfkEvent = ReplicatedStorage:WaitForChild("AFKEvent")

local function focusGained()
	AfkEvent:FireServer(false)
end

local function focusReleased()
	AfkEvent:FireServer(true)
end

UserInputService.WindowFocused:Connect(focusGained)
UserInputService.WindowFocusReleased:Connect(focusReleased)

Hope you can help.

1 Like

Instead of creating the remote event whenever a player is added, create it in roblox studio and delete that line of code.

Still not working I did the thing.


image
image

because the remote event are being sent for the whole server meaning every player also would be better if you just use a command like /afk or something because i see thats its sending for everyone for some reason or another

just a small tweak

		local AfkEvent = Instance.new("RemoteEvent")
		AfkEvent.Parent = ReplicatedStorage

		local function setAfk(player, afk)
			if afk then
				clone.Label.Text = player.Name.." (AFK)"
			else
				clone.Label.Text = player.Name
			end
		end

The problem is that everytime a player joins and their character loads, the gui will be parented to the characters head which means when you change the text it will happen for everybody because they all have the same gui. (Maybe this is the problem)

Apart from removing the creation of a RemoteEvent from a script and having the RemoteEvent pre-made in Studio, change your function to this:

local function setAfk(player, afk)
			if player ~= plr then return end
			if afk then
				clone.Label.Text = player.Name.." (AFK)"
			else
				clone.Label.Text = player.Name
			end
		end

That should fix it. Basically what is happening is that the event is being fired for all clients, and each one is changing their own GUI based on that event. My recommendation is to take this event function outside the PlayerAdded and CharacterAdded functions and modify it to look something like this:

local function setAfk(player, afk)
	local label = player.Character.Head.Overhead.Label
	if afk then
		label.Text = player.Name.." (AFK)"
	else
		label.Text = player.Name
	end
end

The finished script in this scenario should look something like this:

Finished Script
--/ Variables

local ServerStorage = game:GetService("ServerStorage")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Overhead = ServerStorage:WaitForChild("Overhead")

local AfkEvent = ReplicatedStorage:WaitForChild("AFKEvent")

local TweenService = game:GetService("TweenService")

local groupId = "8402934"
local minRank = 125

--/ Functions

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)

		local clone = Overhead:Clone()
		local groupRank = plr:GetRoleInGroup(groupId)
		clone.Parent = character.Head
		clone.Label.Text = plr.Name
		clone.Rank.Text = groupRank
		
		if plr:GetRankInGroup(groupId) >= minRank then
			clone.Icon.Visible = true
		else
			clone.Icon.Visible = false
		end
		if plr:GetRankInGroup(groupId) == 0 then
			clone.Rank.Text = "Customer"
		end	
		
		character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None

		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId,14217559) then
			clone.Parent = character.Head
			clone.Label.Text = plr.Name
			while true do
				local Color = Color3.new(math.random(), math.random(), math.random())
				local ColorTween = TweenService:Create(clone.Label, TweenInfo.new(3), {TextColor3 = Color})
				ColorTween:Play()
				wait(3)

			end
		end
	end)
end)

AfkEvent.OnServerEvent:Connect(function(player, afk)
	local label = player.Character.Head.Overhead.Label
	if afk then
		label.Text = player.Name.." (AFK)"
	else
		label.Text = player.Name
	end
end)

Hopefully this helped.

Thank you Mr Batimius. It really worked. I appreciate you a lot and also the other people that comment on my topic. Thank you, everyone. The thing is now solved.

No problem, glad I could help. I updated the reply to include the full code to the efficient way of doing this. Please keep in mind though that I am on mobile so there might be some errors on there. Hopefully this helped you and I recommend you use the efficient way over your previous way.