Disabling backpack

I want to make it so that the backpack is disabled when I’m in the safezone but that is not working I’ve tried using PlayerGui:SetCoreGuiEnabled

local SafeZoneArea = game.Workspace.SafeZone
local Players = game:GetService("Players")

function CheckIfPlayerIsInArea(character)
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		return humanoidRootPart:IsDescendantOf(SafeZoneArea)
	end
	return false
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		while not character:FindFirstChild("HumanoidRootPart") do
			character:WaitForChild("HumanoidRootPart")
		end

		local function updateBackpack()
			local inSafeZone = CheckIfPlayerIsInArea(character)
			local playerGui = player:WaitForChild("PlayerGui")
			playerGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, not inSafeZone)
		end

		updateBackpack()
		local connection = character.HumanoidRootPart.Touched:Connect(updateBackpack)

		character.AncestryChanged:Connect(function(_, parent)
			if parent == nil then
				connection:Disconnect()
			end
		end)
	end)
end)
3 Likes

is this written in server side

1 Like

i think :SetCoreGuiEnabled dont work with server sided use it with remote event

do you need example script

(i wrote you module type script in bottom)

1 Like

module script in replicated storage (make module name as “BackpackUI”)

local m = {}

function m:CheckIfPlayerIsInArea(plr)
	local humanoidRootPart = plr.Character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		return humanoidRootPart:IsDescendantOf(SafeZoneArea)
	end
	return false
end)

function m:updateBackpack(plr)
	 local inSafeZone = CheckIfPlayerIsInArea(plr)
	 local playerGui = plr:WaitForChild("PlayerGui")

	 playerGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, not inSafeZone)
end)

return m

local script in starter player script

local m = require(game:GetService("ReplicatedStorage").BackpackUI)
local RS = game:GetService("RunService")

local plr = game:GetService("Players").LocalPlayer

RS:Heartbeat:Connect(function()
	 m:updateBackpack(plr)
end)

( this might have few errors because it is written by mobile )

1 Like

I tried this but it didn’t work

1 Like

player added sometimes dont work well
try using print in every section and figure out what is problem

1 Like

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