Attempt to index nil with "PlayerGui"

Hey! I’ve been trying to make a gui visible and tween it on part touch. That works, however, making it tween back and set it to invisible again doesn’t. (Touched and TouchEnded functions.)

The error I’m getting is the exact same what I put as the title.

The script:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:FindFirstChild(hit.Parent.Name)
	if debounce == false then
		debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			local gui = script.ElChapoShop
			local clone = gui:Clone()
			clone.Parent = player.PlayerGui
			clone.ElChapoShop.Visible = true
			clone.ElChapoShop:TweenPosition(UDim2.new(0.5,0,0.5,0),"Out","Sine", 1)
		end
	end
	script.Parent.TouchEnded:Connect(function(hit)
		local player = game.Players:FindFirstChild(hit.Parent.Name)
		if debounce == true and player.PlayerGui:FindFirstChild("ElChapoShop") == false then
			debounce = false
		elseif debounce == true and player.PlayerGui:FindFirstChild("ElChapoShop") == true then
			local gui = player.PlayerGui:FindFirstChild("ElChapoShop")
			gui.ElChapoShop:TweenPosition(UDim2.new(0.5,0,1.5,0),"Out","Sine", 1)
			wait(1)
			gui:Destroy()
			debounce = false
		end
	end)
end)

Always use :WaitForChild() when getting the PlayerGui from a LocalScript.

local PlayerGui = Player:WaitForChild("PlayerGui")

Use GetPlayerFromCharacter when getting the Player, you should also check if you are actually getting the Player with an if statement, because hit.Parent does not ensure a Model.

This means you didn’t get the player correctly, player is nil in this context

You need to firstly check that it was the player who touched it, using something like:

if hit.Parent:FindFirstChild("Humanoid") then
--code
end

Yeah I did that in the first function… No point in doing it in the TouchEnded function.

Try to define the player inside that if statement.

(Also, I might be wrong, but it’s probably still a good idea to check in the .TouchEnded event.)

Okay so, I added it to the TouchEnded function aswell, however it still gives an error.

I see many issues in the code, I just did a small edition and added a couple of comments:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if not debounce then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			debounce = true
			if hit.Parent:FindFirstChild("Humanoid") then
				local gui = script.ElChapoShop
				local clone = gui:Clone()
				clone.Parent = player.PlayerGui
				clone.ElChapoShop.Visible = true
				clone.ElChapoShop:TweenPosition(UDim2.new(0.5,0,0.5,0),"Out","Sine", 1)
			end
		end		
	end
end)


script.Parent.TouchEnded:Connect(function(hit)
	if debounce then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			if player:FindFirstChild("PlayerGui") then
				if player.PlayerGui:FindFirstChild("ElChapoShop") then -- Check if theres a ScreenGui Called ElChapoShop?
					debounce = false			
				else
					local gui = player.PlayerGui:FindFirstChild("ElChapoShop")
					gui.ElChapoShop:TweenPosition(UDim2.new(0.5,0,1.5,0),"Out","Sine", 1) -- Tween a "Frame" called ElChapoShop inside a ScreenGui called ElChapoShop too?...
					task.wait(1) -- Wait until the Tween finished not a "random" 1 second
					gui:Destroy()
					debounce = false
				end
			end
		end
	end
end)
2 Likes

Had to edit it a little bit to my needs, however works perfectly now. Thank you so much!

1 Like

Awesome! El Chapo will be happy with that :v :stuck_out_tongue_winking_eye:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.