Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt for index nil with player gui and player not being kicked?

I was making an admin panel and here is the code with 2 errors:

script.Parent.BanButton.MouseButton1Click:Connect(function(player)
	
	local playerGui = player.PlayerGui
	
	local playerNameBox = script.Parent.PlayerNameBox
	local banReasonBox = script.Parent.BanReasonBox
	
	local playerToBan = game.Players:FindFirstChild(playerNameBox.Text)
	
	playerToBan.Banned.Value = true
	
	playerToBan.Banned.BanReason.Value = banReasonBox.Text
	
	if playerNameBox.Text == "Name" then
		playerGui.BanPanel.MainFrame.BanButton.Text = "Please enter a player to ban"
	elseif banReasonBox.Text == "Reason" then
		playerGui.BanPanel.MainFrame.BanButton.Text = "Please enter a reason"
	else
		playerToBan:Kick(banReasonBox.Text) -- Kicks player for the givin reason
	end
end)

The first error is on line 15 and 17. "Attempt to index nil with 'PlayerGui'" Another error I got before I added was with the kick part. It is supposed to kick the player in the banReasonBox. Note: This is a local script

What is the problem? Thanks!

3 answers

✓ Accepted answer

Your supposed to reference the player using:

local Player = game.Players.LocalPlayer

Also keep in mind to check if the player thats inputted exists. It might lead to errors later.

if playerToBan then
	playerToBan:Kick(banReasonBox.Text)
end

Have a great day!

I don't think player is a parameter of mousebutton1click.

1 comment

0

you could do local player = script.Parent.Parent.Parent or somethin like that.

at the 3rd line, change

local playerGui = player.PlayerGui

to

local playerGui = game.Players.LocalPlayer.PlayerGui

this is because player is not a parameter of Gui.MouseButton1Click event. Instead, it is a parameter for ClickDetector.MouseClick event.

This discussion is preserved as it was. New answers, comments, and votes are closed.