Scripting issue

Hello!
I would like that only those that are allowed can see the button while anyone else cannot.
The issue is I have tried to do so but it does not work and I get this error:

attempt to index nil with ‘FindFirstChild’

I’ve tried to fix it but I cannot. Help would be appreciated.
By the way, I am aware about the ID “issue”.

local Players = game:GetService("Players")
local permittedIDs = 0
local respawnGui = Players:FindFirstChild("PlayerGui"):FindFirstChild("RespawnGui")

Players.PlayerAdded:Connect(function(plr)
	if plr.UserId == permittedIDs then
		plr:FindFirstChild("PlayerGui"):FindFirstChild("RespawnGui").Enabled = true
		print(plr.UserId .. " is permitted, the button is visible.")
	else
		plr:FindFirstChild("PlayerGui"):FindFirstChild("RespawnGui").Enabled = false
		print(plr.UserId .. " is not permitted, the button is hidden.")
	end
	if plr.UserId ~= permittedIDs and plr:FindFirstChild("PlayerGui"):FindFirstChild("RespawnGui").Enabled == true then
		plr:Kick()
	end
end)

Jesus loves you :smile:

1 Like

PlayerGui is not a member of game.Players by default, try removing this line

1 Like

The issue lies in line 3 as @JamiePro248clone said, you’d have to reference the playerGui through the local player. (unless this is a server script which in that case, LocalPlayer isn’t a thing on the server, i have no idea why you’d use PlayerAdded on the client side)

local Players = game:GetService("Players")
local permittedIDs = 0
local respawnGui = Players.LocalPlayer:FindFirstChild("PlayerGui"):FindFirstChild("RespawnGui")

if this is a server script, then you’d use the PlayerAdded() player parameter.

local Players = game:GetService("Players")
local permittedIDs = 0

Players.PlayerAdded:Connect(function(plr)
    local respawnGui = plr.PlayerGui:FindFirstChild("RespawnGui")
    respawnGui.Enabled = if plr.UserId == permittedIDs then true else false

	if plr.UserId ~= permittedIDs and respawnGui.Enabled then
		plr:Kick()
	end
end)
1 Like

It is a server script. Wouldn’t clients be able to exploit it if it were a local script?

1 Like

Line three

local respawnGui = Players:FindFirstChild("PlayerGui"):FindFirstChild("RespawnGui")

It should be

local respawnGui = game:GetService("StarterGui"):WaitForChild("RespawnGui")
1 Like

Make sure plr isn’t nil.

Never control UI through a server script, there isn’t any need to. It only slows the server down when done in a mass scale. I’ve seen this happen in one of the games I’ve worked on in the past.

Anyhow, it would be in your best interest and practice to switch to a LocalScript. However you try, the player can always make the button visible and click on it. I would suggest not doing anything on the client to stop unintended behaviour.

If you’re using RemoteEvents/RemoteFunctions, only then would you check if the user is permitted in the Script that receives these events and kick them accordingly.

1 Like