Script randomly stopped working

In my game, there is an area called a campsite. Players, assuming the campsite is their property, can enter this area and receive a gun. This gun is removed from their inventory upon exiting the area. I made this using Region3, and it has always worked, until recently. Without ever changing anything, I tested the campsite and the player still receives the gun, but it is constantly being removed from the player’s inventory and readded, despite the player not leaving the area. There are no errors in output. I have tried a lot of things that have all been unsuccessful, including changing the character part that is detected by the area from HumanoidRootPart to Torso and Head and disabling other scripts that could have caused the issue. I have a pretty vast knowledge of scripting, but I have never encountered anything like this. If anyone could help me, I would greatly appreciate it.

local Campsite = script.Parent
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.Character:WaitForChild("Property").Value == "Campsite1" 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.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)
2 Likes

Did you possibly change where the Campsite Part is, or rename it in another script so that it doesn’t recognize "Campsite1"?

Nope, neither of those things.

Maybe it’s outdated, or you changed something in studio, by accident? [Weird]

That’s mostly because of a bug where the game acts like the HumanoidRootPart isn’t in the campsite or whatever it is

I don’t have any solution or a fix for this

See if this solves your issue:

local Campsite = script.Parent
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.Character:WaitForChild("Property").Value == "Campsite1" then
		local Character = player.Character:WaitForChild("HumanoidRootPart")
		local Backpack = player.Backpack
		--
		local partsInRegion, foundCharacter, foundGun;
		--
		while wait() do
			partsInRegion = workspace:FindPartsInRegion3(Region)
			foundCharacter = table.find(partsInRegion, Character)
			--
			foundGun = Backpack:FindFirstChild("Gun") or Character.Parent:FindFirstChild("Gun")
			--
			if foundCharacter and not foundGun then
				game.ServerStorage.Gun:Clone().Parent = Backpack
			elseif not foundCharacter and foundGun then
				foundGun:Destroy()
			end
		end
	end
end)

Simplified the logic to ensure it wouldn’t alternate back and fourth. See how it works :smiley:

1 Like

Try a couple of print() statements for troubleshooting in your if table.find loop to see what information is/isn’t being passed.