Remote event returning nil Server --> Client

Recently, I’ve been making a billboard GUI that displays whether a player is AFK or not, if they are there will be a text label that is displayed above them. Works perfectly, just I want to make it so only certain people can see it. I’ve been told to use :FireClient() unsure if this is the right move but it seems to be working. Here is the code I have so far.

Script

local nametag = script:WaitForChild("AFKGui")
c = 1
local event = game.ReplicatedStorage.Events.ShowGui

game.Players.PlayerAdded:Connect(function(player)
	print(player,"      x2")
	event:FireClient(player)
	player.CharacterAdded:Connect(function(character)
		c = nametag:Clone()
		c.Parent = character.Head
	end)
end)

Local Script

local event = game.ReplicatedStorage.Events.ShowGui
local list = {"VaIuent"}
	
event.OnClientEvent:Connect(function(plr)
	for _,v in pairs(list) do
		print(plr," ", v)
		if plr == v then
			script.Parent.Enabled = true
		else
			script.Parent.Enabled = false
		end
	end
end)

I added the print function in both scripts to see what is going on. The server script prints: VaIuent x2 like it’s supposed to. But the Local Script, prints: nil VaIuent why is the player parameter coming up as nil, I’m sure I made some foolish mistake in the code but just unsure on how to fix it.

For reference, here is the parent of everything. And as always, I thank you all for any help you may give me in advance! :grinning_face_with_smiling_eyes:

I think it is because when you fireclient with a plr parament it is just firing the event to that player and cannot be picked up on the client.

Sorry I’m not too sure what you mean by this, kinda new to scripting, do you mean like it’s just being redirected back?

In the local script, the OnClientEvent shouldn’t have the plr parameter in the first place. You would get the plr simply by doing plr = game.Players.LocalPlayer and changing it to event.OnClientEvent:Connect(function().

2 Likes

So, you can’t get the .OnClientEvent(player) because when you do :FireClient(player) it only fires the event to that player, NOT putting it as parameter. (Don’t mind bad grammar).

Your script should be

local event = game.ReplicatedStorage.Events.ShowGui
local list = {"VaIuent"}
local plr = game.Players.LocalPlayer
	
event.OnClientEvent:Connect(function()
	for _,v in pairs(list) do
		print(plr," ", v)
		if plr == v then
			script.Parent.Enabled = true
		else
			script.Parent.Enabled = false
		end
	end
end)
1 Like
local nametag = script:WaitForChild("AFKGui")
c = 1
local event = game.ReplicatedStorage.Events.ShowGui

game.Players.PlayerAdded:Connect(function(player)
	print(player.Name,"      x2")
	event:FireClient()
	player.CharacterAdded:Connect(function(character)
		c = nametag:Clone()
		c.Parent = character.Head
	end)
end)
local event = game.ReplicatedStorage.Events.ShowGui
local list = {"VaIuent"}
	
event.OnClientEvent:Connect(function()
    local plr = game.Players.LocalPlayer
	for _,v in pairs(list) do
		print(plr.Name," ", v)
		if plr == v then
			script.Parent.Enabled = true
		else
			script.Parent.Enabled = false
		end
	end
end)

Without the player in FireClient it won’t fire to anyone, unless ofc you do FireAllClients

Looking at your code, you probably don’t even need a OnClientEvent. Try using this instead (inside the local script):

local list = {"VaIuent"}
local plr = game.Players.LocalPlayer
script.Parent.Enabled = table.find(list, plr.Name) ~= nil

I see but now I receive this error for :FireClient() || FireClient: player argument must be a Player object.

This works but it still for some reason makes my Billboard GUI disabled. Even though the 2 outputs are the same.

Fixed this, all I had to do was add plr.Name. Let me just Team Test this to make sure it works.

local nametag = script:WaitForChild("AFKGui")
c = 1
local event = game.ReplicatedStorage.Events.ShowGui

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
		c = nametag:Clone()
		c.Parent = character.Head
	end)
	print(player,"      x2")
	event:FireClient(player)
end)
local event = game.ReplicatedStorage.Events.ShowGui
local list = {"VaIuent"}
local plr = game.Players.LocalPlayer
	
event.OnClientEvent:Connect(function()
	for _,v in pairs(list) do
		print(plr.Name," ", v)
		if plr.Name == v then
			script.Parent.Enabled = true
		else
			script.Parent.Enabled = false
		end
	end
end)

This should all work. I fixed the issue you were having with the player checking.

This works but it’s only local, my friend can still see the AFKTag above my head, is there any way to make it so it’ll disable all of them locally if your name doesn’t match the list?

Honestly you don’t even need the client script.

Server Script:

local nametag = script:WaitForChild("AFKGui")
c = 1
local event = game.ReplicatedStorage.Events.ShowGui
local list = {"VaIuent"}

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
		c = nametag:Clone()
        for i,v in pairs(list) do
            if plr.Name == v then
                c.Enabled = true
            else
                c.Enabled = false
            end
		c.Parent = character.Head
	end)
	print(player,"      x2")
end)

Still doesn’t work, I can see mine, but he can see mine as well and he’s not in the list.

Oh, I misread your reply originally, mb.

local nametag = script:WaitForChild("AFKGui")
c = 1
local event = game.ReplicatedStorage.Events.ShowGui

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
		c = nametag:Clone()
        c.Enabled = false
		c.Parent = character.Head
	end)
	print(player,"      x2")
	event:FireClient(player)
end)
local event = game.ReplicatedStorage.Events.ShowGui
local list = {"VaIuent"}
local plr = game.Players.LocalPlayer
	
event.OnClientEvent:Connect(function()
	for _,v in pairs(list) do
		print(plr.Name," ", v)
		if plr.Name == v then
			script.Parent.Enabled = true
		else
			script.Parent.Enabled = false
		end
	end
end)

Try this

This works, I can see when I’m AFK and he can’t, but I still cannot see when he’s AFK, the tag doesn’t pop up for either of us for him.

I tried this for the Local Script, same output.

event.OnClientEvent:Connect(function()
	for _,i in pairs(players:GetPlayers()) do
		for _,v in pairs(list) do
			print(plr.Name," ", v)
			if plr.Name == v then
				i.Character.Head.AFKGui.Enabled = true
			else
				i.Character.Head.AFKGui.Enabled = false
			end
		end
	end
end)