You can write your topic however you want, but you need to answer these questions:
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
What is the issue? It says attempt to index nil with ‘PlayerGui’ when i try and set a text to the rappers text.
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
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
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)