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

Why did it give me an error saying: attempt to index nil with 'PlayerGui'?

The cause of the error could be found at line 14

local seat = script.Parent
local sound = script.Parent.On
local off = script.Parent.Off
local player = game.Players.LocalPlayer
local ui = script.Parent:FindFirstChild("PoliceGUI")

seat.Changed:Connect(function(ch)
	if ch == "Occupant" then
		if seat.Occupant then
			local player = game.Players:FindFirstChild(seat.Occupant.Parent.Name)
			ui:Clone().Parent = player.PlayerGui
			sound:Play()
		end
		player.PlayerGui:FindFirstChild("PoliceGUI"):Destroy()
		off:Play()
	end
end)

1 comment

0

GetPlayerFromCharacter(seat.Occupant.Parent)?

1 answer

✓ Accepted answer

If this is a Script and not a LocalScript (which I assume is the case, based on your code) then Players.LocalPlayer is nil. There's no concept of LocalPlayer on the server, and it wouldn't make sense for such a thing to exist.

You're going to have to find a solution to your code without LocalPlayer. If you want something to happen with some or all players, you can make use of GetPlayers. If you want your code to execute in a LocalScript when something happens in a Script, you can try using Remote Events.

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