Trying to disable a player's inventory when they hit a part, but if they hit a different part, their inventory is enabled

hey fellas.

im trying to make a player’s inventory disable when they hit a part, but re enable if they hit a different part.

here is my code:

local Players = game:GetService("Players")
local CoreGui = game:GetService("StarterGui") --the service

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Player") then
		game.LocalPlayer.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	end
end)

and the one that enables it:

local Players = game:GetService("Players")
local CoreGui = game:GetService("StarterGui") --the service

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Player") then
		game.LocalPlayer.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
	end
end)

neither of these are working. What am i doing wrong?

If this is a localscript inside of a part, it won’t run. Instead, maybe you try to use use a script in the part with a remote event telling the client to disable their inventory.

1 Like

i thought about that, but when i tried to put it in a server script the output said it had to be a local script to disable core gui.

That is true, it does have to be a localscript in order to manipulate the core gui. However, you can get around this by using remote events. When the part is touched, the server side script would fire a remote event which tells the client (through a localscript) that they need to disable that part of their core gui. You can read about remote events here!

1 Like