Can anyone find anything wrong with this script? It is supposed to give a player a gun when they enter a region and remove it when they leave. It used to work and still kinda works but the gun keeps getting removed and readded whenever the player moves around (they aren’t leaving the region). There are no errors in output.
local Campsite = script.Parent.Campsite
local Pos1 = Campsite.Position - (Campsite.Size / 2)
local Pos2 = Campsite.Position + (Campsite.Size / 2)
local Region = Region3.new(Pos1, Pos2)
game.Teams.Campers.PlayerAdded:Connect(function(player)
if player:GetAttribute("Property") == script.Parent.Name then
local Character = player.Character:WaitForChild("HumanoidRootPart")
local Backpack = player.Backpack
while wait() do
local partsInRegion = workspace:FindPartsInRegion3(Region)
if table.find(partsInRegion, Character) and #Backpack:GetChildren() == 0 and not Character.Parent:FindFirstChild("Gun") then
game.ServerStorage.Tools.Gun:Clone().Parent = Backpack
elseif not table.find(partsInRegion, Character) then
Backpack:ClearAllChildren()
for _, object in pairs(Character.Parent:GetChildren()) do if object:IsA("Tool") then object:Destroy() end end
end
end
end
end)