Attempt to index nil with 'PlayerGui'

The script works the first time but after that it doesn’t work. It gives this error and I have had it before but somehow solved it. I have two questions… How to I fix the problem for right now and what should I look for if i see this error again?

script.Parent.Touched:Connect(function(otherpart)
	local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)
	local PGUI = player.PlayerGui -- here is error
	local PopUp = PGUI.DoorPopUp.FramePopUp	
	PopUp.Visible = true		
			
	end)

1 Like
script.Parent.Touched:Connect(function(otherpart)
	local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)
    if player then
	local PGUI = player.PlayerGui -- here is error
	local PopUp = PGUI.DoorPopUp.FramePopUp	
	    PopUp.Visible = true		
    end
end)

I recommend you check if the player is actually given or not

script.Parent.Touched:Connect(function(otherpart)
	local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)
	if not player then return end
	local PGUI = player.PlayerGui 
	local PopUp = PGUI.DoorPopUp.FramePopUp	
	PopUp.Visible = true				
end)

Without a check it’ll jsut error. Something like this will do nothing if a player was not found

@GoteeSign Your code will still error because you do the check after it tries to get the PlayerGui from nil

3 Likes

Yeah thanks, I fixed it now.

1 Like

Its working but for some reason when I try and touch it again It doesn’t make the gui visible again

Are you closing it on the client? If so, that’s probably why as on your screen it’s closed but for the server it still sees it as open.

I think it may be better to convert it into a localscript (in somewhere such as StarterPlayerScripts as they do not work in workspace) and do some touch verification so it only opens for you and not for others

Here is something you can try. You want to set up a remote event in Replicated Storage (name it “TouchedEvent”) so that the server and client scripts can communicate with each other. When you’re done that, go in your current server script, delete everything except

script.Parent.Touched:Connect(function(otherpart)
-- Delete everything inside this
end)

Then you want to add this code inside the function:

game.ReplicatedStorage.TouchedEvent:FireClient(otherpart)

After that, go to the PopUp Gui, and insert a local script inside the ScreenGui (not inside the frames). You want to add the following code:

local Event = game.ReplicatedStorage.TouchedEvent
local function onTouchedPart(otherpart)
	local player = game.Players:GetPlayersFromCharacter(otherpart.Parent)
	local PopUp = script.Parent.DoorPopUp.FramePopUp
	PopUp.Visible = true
end
Event.OnClientEvent:Connect(onTouchedPart)

Hopefully this helped!

make a new line under the player variable and put, if player then.

script.Parent.Touched:Connect(function(otherpart)
	local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)
	if player then
		local PGUI = player.PlayerGui
		local PopUp = PGUI.DoorPopUp.FramePopUp	
		PopUp.Visible = true		
	end
end)

Hope it works :crossed_fingers: