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!
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().
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)
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)
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?
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)
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)
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)