player = script.Parent.Parent.Parent
mouse = player:GetMouse()
invBox = game.StarterGui.ScreenGui.Frame
local open = false

function inv(key)
	key = key:lower()
	
	if key == "p" then
		if open then
		player.PlayerGui.ScreenGui.Frame.Visible = false -- You can change "Frame" or "ScreenGui" to your Gui's name(s)
		open = true	
		open = false
		return 
		end
		if not open then
		player.PlayerGui.ScreenGui.Frame.Visible = true -- You can change "Frame" or "ScreenGui" to your Gui's name(s)
		open = true	
		end
	end
	
end

mouse.KeyDown:connect(inv)

If I were to put the script into PlayerScripts then it would work just fine

if I make player = script.Parent.Parent