Attempt to index nil with 'PlayerGui'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? So I’m trying to make a rap battles game and I’m stuck on the rappers chat thingy

  2. What is the issue? It says attempt to index nil with ‘PlayerGui’ when i try and set a text to the rappers text.

  3. What solutions have you tried so far? None

	rapper.Chatted:Connect(function(chat)
		player.PlayerGui.RappersChat.Chat.Text = chat
	end)
end

function AddPlayerToStage()
	local rapper = PickRandomPlayer()
	local PlayerSpawn = math.random(1, 2)
	if PlayerSpawn == 1 then
		rapper.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.Spawn1.CFrame
		rapper.Character:WaitForChild("HumanoidRootPart").Anchored = true
	elseif PlayerSpawn == 2 then
		rapper.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.Spawn2.CFrame
		player.Character:WaitForChild("HumanoidRootPart").Anchored = true
	end
	rapper.Character.Parent = workspace.Rappers
	while rapper.Character.Parent == workspace.Rappers do
		ShowRappersChat(rapper)
		wait()
	end
end

Thanks if you have a solution

Replace player.PlayerGui with rapper.PlayerGui.

no im trying to show it for all players

What is player equal to? and is this a Script or a LocalScript?

full script if that helps. ```-- Services
local Players = game:GetService(“Players”)

function PickRandomPlayer()
local PlayersInGame = game.Players:GetChildren()
local randomPlayer = PlayersInGame[math.random(1,#PlayersInGame)]

return randomPlayer

end

function ShowRappersChat(rapper)
local player = game.Players.LocalPlayer
rapper.Chatted:Connect(function(chat)
player.PlayerGui.RappersChat.Chat.Text = chat
end)
end

function AddPlayerToStage()
local rapper = PickRandomPlayer()
local PlayerSpawn = math.random(1, 2)
if PlayerSpawn == 1 then
rapper.Character:WaitForChild(“HumanoidRootPart”).CFrame = workspace.Spawn1.CFrame
rapper.Character:WaitForChild(“HumanoidRootPart”).Anchored = true
elseif PlayerSpawn == 2 then
rapper.Character:WaitForChild(“HumanoidRootPart”).CFrame = workspace.Spawn2.CFrame
rapper.Character:WaitForChild(“HumanoidRootPart”).Anchored = true
end
rapper.Character.Parent = workspace.Rappers
while rapper.Character.Parent == workspace.Rappers do
ShowRappersChat(rapper)
wait()
end
end

while true do
wait(5)

AddPlayerToStage()

end```

server script in serverscriptservice.

LocalPlayer doesn’t exist inside server scripts, therefore you must loop through each individual player:

rapper.Chatted:Connect(function(chat)
	for _, player in pairs(game.Players:GetPlayers()) do 
		player.PlayerGui.RappersChat.Chat.Text = chat
	end
end)

there is now a error… 19:54:48.666 ServerScriptService.Main:23: attempt to index nil with 'WaitForChild' - Server - Main:23

script: function AddPlayerToStage() local rapper = PickRandomPlayer() local PlayerSpawn = math.random(1, 2) if PlayerSpawn == 1 then rapper.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.Spawn1.CFrame -- error here rapper.Character:WaitForChild("HumanoidRootPart").Anchored = true elseif PlayerSpawn == 2 then rapper.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.Spawn2.CFrame rapper.Character:WaitForChild("HumanoidRootPart").Anchored = true end rapper.Character.Parent = workspace.Rappers while rapper.Character.Parent == workspace.Rappers do ShowRappersChat(rapper) wait() end end

nvm fixed it, added a repeat wait() until rapper.Character before if PlayerSpawn == 1 then
rapper.Character:WaitForChild(“HumanoidRootPart”).CFrame = workspace.Spawn1.CFrame
rapper.Character:WaitForChild(“HumanoidRootPart”).Anchored = true
elseif PlayerSpawn == 2 then
rapper.Character:WaitForChild(“HumanoidRootPart”).CFrame = workspace.Spawn2.CFrame
rapper.Character:WaitForChild(“HumanoidRootPart”).Anchored = true
end

The script will still most-likely not work due to the UI being loaded on the client(which means the server is unable to see it), to work around this you must use a Remote Event(specifically Remote:FireAllClients(chat)) and have a client script pick it up:

--a LocalScript
local Remote = game.ReplicatedStorage:WaitForChild("UIRemote") --create a remote named "UIRemote"
local player = game.Players.LocalPlayer

Remote.OnClientEvent:Connect(function(chat)
	player.PlayerGui.RappersChat.Chat.Text = chat
end)
--the server script
local Remote = game.ReplicatedStorage:WaitForChild("UIRemote")

--the part of your code causing the issue
rapper.Chatted:Connect(function(chat)
	Remote:FireAllClients(chat)
end)
1 Like

thanks, i think this is wrapped up.