Attempt to index nil with 'PlayerGui'

I was trying to make a Admin script and I got an error when I tried to kick myself using the command bar.

There is a RemoteEvent named CommandEvent and CommandEvent’s parent is ReplicatedStorage. There is a script in ServerScriptService named MainAdminScript.
And here is the gui:

image

Here’s the script in ServerScriptService:

local rs = game:GetService("ReplicatedStorage")
local commandEvent = rs:WaitForChild("CommandEvent")
local player = game:GetService("Players").LocalPlayer

local actions = {

	"kill"

}

local function valid_command(action)
	for _,command in pairs(actions) do
		if command == action then
			return true
		end
	end
end

local function valid_player(person)
	local player = game.Players:FindFirstChild(person)
	if player then
		return true
	end
end

local function kill_player(person)
	local player = game.Players:FindFirstChild(person)
	player.Character.Humanoid.Health = 0
end

local function kick_player(person)
	player:Kick("You have been kicked by: "..player.Name)
end

local function do_command(player, action, person)
	if valid_command(action) and valid_player(person) then
		if action == "kill" then
			kill_player(person)
		end
		if action == "kick" then
			kick_player(person)
		end
	else
		game.Players.LocalPlayer.PlayerGui:WaitForChild("Admin").MainFrame.CommandBox.TextColor3 = Color3.fromRGB(255, 0, 0)
		game.Players.LocalPlayer.PlayerGui:WaitForChild("Admin").MainFrame.CommandBox.Text = "Error: Invalid Command"
		wait(2.5)
		game.Players.LocalPlayer.PlayerGui:WaitForChild("Admin").MainFrame.CommandBox.TextColor3 = Color3.fromRGB(255, 255, 255)
		game.Players.LocalPlayer.PlayerGui:WaitForChild("Admin").MainFrame.CommandBox.Text = ""
	end
end

commandEvent.OnServerEvent:Connect(do_command)

Here’s the script in the gui:

local rs = game:GetService("ReplicatedStorage")
local commandEvent = rs:WaitForChild("CommandEvent")

local CommandBox = script.Parent
local Enter = script.Parent.Parent.Enter

local function Enter2()
	local command = {}
	for word in string.gmatch(CommandBox.Text, "%S+") do
		table.insert(command, word)
	end
	local action = command[1]
	local person = command[2]
	commandEvent:FireServer(action, person)
	CommandBox.Text = ""
end

Enter.MouseButton1Down:connect(Enter2)

And here’s the error in the output:

Line 44 in MainAdminScript is:

		game.Players.LocalPlayer.PlayerGui:WaitForChild("Admin").MainFrame.CommandBox.TextColor3 = Color3.fromRGB(255, 0, 0)
2 Likes

Wait actually don’t reply back please.

1 Like

I think I know the problem.

I need 30 words in this text remember?

You can’t acces localplayer or playergui from server scripts.

You can still access someone’s playergui, you just can’t get a reference to a player by doing localplayer

You can’t do Players.LocalPlayer, it will return nil. You have to manually index the player from Players:GetPlayers() and access PlayerGui from there.

What I meant to say was that you can still access someone’s playergui from a server script, you just need to find a different method to get the player than localplayer

Which I don’t know. I know only how to access PlayerGui using LocalScript but as you can see I need it to be through a script. And I don’t really know how to use RemoteEvents but I think you can get the LocalPlayer through a RemoteEvent firing a LocalScript in StarterGui.

I don’t know if this works.

Thats wt I told u, use a remote event and inside a for loop check if any of the players in GetPlayers() is equivalent ti the player who fired the event, and then u can access the players playergui.

I don’t understand.

Ignore this.

Wait a second I will dm u with the answer along with the explanation.

Ok thanks! I appreciate that!

Again ignore this because my message is too short.

What’s the point of doing a for loop, you already have the player right there.

To op: Instead of doing game.Players.LocalPlayer, just do player.

local rs = game:GetService("ReplicatedStorage")
local commandEvent = rs:WaitForChild("CommandEvent")

local actions = {

	"kill"

}

local function valid_command(action)
	for _,command in pairs(actions) do
		if command == action then
			return true
		end
	end
end

local function valid_player(person)
	local player = game.Players:FindFirstChild(person)
	if player then
		return true
	end
end

local function kill_player(person)
	local player = game.Players:FindFirstChild(person)
	player.Character.Humanoid.Health = 0
end

local function kick_player(person)
	player:Kick("You have been kicked by: "..player.Name)
end

local function do_command(player, action, person)
	if valid_command(action) and valid_player(person) then
		if action == "kill" then
			kill_player(person)
		end
		if action == "kick" then
			kick_player(person)
		end
	else
	    local playergui = player:WaitForChild("PlayerGui")
		playerGui:WaitForChild("Admin").MainFrame.CommandBox.TextColor3 = Color3.fromRGB(255, 0, 0)
        playerGui:WaitForChild("Admin").MainFrame.CommandBox.Text = "Error: Invalid Command"
		wait(2.5)
		playerGui:WaitForChild("Admin").MainFrame.CommandBox.TextColor3 = Color3.fromRGB(255, 255, 255)
		playerGui:WaitForChild("Admin").MainFrame.CommandBox.Text = ""
	end
end

commandEvent.OnServerEvent:Connect(do_command)
1 Like

Yes tht was a mistake on my part, sry abt tht.

2 Likes

I still have a error I mean like a warning:

image
image

1 Like
local rs = game:GetService("ReplicatedStorage")
local commandEvent = rs:WaitForChild("CommandEvent")

local actions = {

	"kill"

}

local function valid_command(action)
	for _,command in pairs(actions) do
		if command == action then
			return true
		end
	end
end

local function valid_player(person)
	local player = game.Players:FindFirstChild(person)
	if player then
		return true
	end
end

local function kill_player(person, player)
	local player = game.Players:FindFirstChild(person)
	player.Character.Humanoid.Health = 0
end

local function kick_player(person, player)
	player:Kick("You have been kicked by: "..player.Name)
end

local function do_command(player, action, person)
	if valid_command(action) and valid_player(person) then
		if action == "kill" then
			kill_player(person, player)
		end
		if action == "kick" then
			kick_player(person, player)
		end
	else
	    local playergui = player:WaitForChild("PlayerGui")
		playerGui:WaitForChild("Admin").MainFrame.CommandBox.TextColor3 = Color3.fromRGB(255, 0, 0)
        playerGui:WaitForChild("Admin").MainFrame.CommandBox.Text = "Error: Invalid Command"
		wait(2.5)
		playerGui:WaitForChild("Admin").MainFrame.CommandBox.TextColor3 = Color3.fromRGB(255, 255, 255)
		playerGui:WaitForChild("Admin").MainFrame.CommandBox.Text = ""
	end
end

commandEvent.OnServerEvent:Connect(do_command)

One last warning:

image

Oh wait I see the warning problem I’ll fix that and try if it works now!

Its a variable error, pls change playergui into playerGui.