Checking instances inside Region3

So basically, I am making a swordfight area where you get a sword while being there.
Problem is that the code that checks if a player left the area is always running, not allowing the player to have a sword at all.

local repStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local Environment = workspace:WaitForChild("Environment")
local Gameplay = workspace:WaitForChild("Gameplay")
local regionblock = Gameplay.Regions.SwordfightRegion
local ignoredInstances = Environment.MainIsland.Swordfight:GetChildren()
local modules = repStorage.modules

local sfRegion = (regionblock.Position - regionblock.Size / 2) , (regionblock.Position + regionblock.Size/2) )

local isInRegion = require(modules.swordfighters)

local sword = game:GetService("ServerStorage").Sword

while true do
	local insideRegion = workspace:FindPartsInRegion3WithIgnoreList(sfRegion, ignoredInstances)
	for i, instance in pairs(insideRegion) do
		local hum = instance.Parent:FindFirstChild("Humanoid")
		if hum then
			local char = hum.Parent
			local plr = players:GetPlayerFromCharacter(char)
			if not isInRegion[plr] then
				isInRegion[plr] = plr
				local newSword = sword:Clone()
				sword.Parent = plr.Backpack
	for i, plr in pairs(isInRegion) do
		local char = plr.Character
		if insideRegion[plr] == nil then
			local sword = char:FindFirstChild("Sword")
			local sword2 = plr.Backpack:FindFirstChild("Sword")
			if sword then
			elseif sword2 then
			isInRegion[plr] = nil

insideRegion table always returns nil for some reason.

I suggest using zone plus, more performant only detects what’s needed, and easy player entered and exited events.

1 Like

Great tool! I will probably swich to that instead of my already existing method but I’d like to know what’s going wrong with my current code so I won’t repeat the same mistake in the future. :sweat_smile:

Anyways thanks a lot for showing me that tool I will be definitely using that!

I think you got your tables mixed up you are checking the inside region rather than is in region. The naming is pretty confusing as well should be players inside region and instances inside region to avoid mixing them up.

1 Like