Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
CodeWon 181
3 years ago

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

Log in to vote
1
Answered by
Ludzik78 136
3 years ago

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!

Ad
Log in to vote
0
Answered by 3 years ago

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

0
you could do local player = script.Parent.Parent.Parent or somethin like that. BulletproofVast 1033 — 3y
Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago

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.

Answer this question