Detect if tool is in Region3 with use of module script

Hey

So i’m trying to do a damage script. But with region3.
So basically i created a Region3 around an enemy.

region3

local regionPart = script.Parent
local pos1 = regionPart.Position - (regionPart.Size/2)
local pos2 = regionPart.Position + (regionPart.Size/2)
local region = Region3.new(pos1, pos2)

What im trying to do is whenever i attack, and my sword is in that region, it damages the NPC.
I’m trying to use a module script, to store my tools data in.

module script:

local tools= {
	
	["Wooden Sword"] = {
		["Damage"] = 15
	},
	
	["Test"] = {
		["Damage"] = 10
	}
}

return tools

I’m trying to check if “Wooden Sword” or any tool i add in the future is in that region, to damage the npc.

while true do
	wait()
	local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region, ToolsModule, 500)
	for tools, part in pairs(partsInRegion) do
		if EHum and tools then
			print("Enemy found")
		end
	end
end

As you can see i tried to whitelist the module script, hoping it would whitelist every tool i add. But i don’t think it works like that.

Then i tried creating a table and adding all the Tools in that.

local tools = {}
for keyName, keyValue in pairs(ToolsModule) do
	table.insert(tools, keyName)
end

And changed to

while true do
	wait()
	local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region, tools, 500)
	for _, part in pairs(partsInRegion) do
		if EHum then
			print("Enemy found")
		end
	end
end

But with that i get an error:
‘Unable to cast value to Object’

I thought workspace:FindPartsInRegion3WithWhiteList needed a table?

Well, now i have no idea what to try next.
Sorry if this is unclear. But if you have any ideas, i’d like to hear them.

I’m extremely new to region3 and module scripts.

I just realised i never actually did anything with my tool.
Mhm, i still have no idea how to fix it.

Thank you for reading.

1 Like

Is this error coming from the partsInRegion variable? If it is, then it’s because the stuff inside of the tools table are tables from the tools module not objects.

Instead, you could probably change the script like this:

while true do
	wait()
	local partsInRegion = workspace:FindPartsInRegion3(region, nil, 500)
	for tools, part in pairs(partsInRegion) do
		if EHum and part.Name == "Handle" then
			if table.find(tools,part.Parent.Name) ~= nil then
				print("Enemy found!")
			end
		end
	end
end

I’m not 100% sure if this will work.

this kinda works, but why is here:

if EHum and part.Name == "Handle" then
        print(part.Name)
end

why is part.Name = Handle?

When my character is in the region in prints “Handle”.
Even when my tool isn’t equipped.
I don’t understand why that is.

Edit: i was able to achieve what i needed.

~= nil

This does nothing by the way, I see a lot of people use it for some reason. If something is not nil then it is considered truthy in terms of Boolean expression logic.