Check if a certain part is inside of an object

I am making a shield spell and need to check if the part that touches the shield is part of a folder called Attacks. I am currently trying to use the table.find() function to see if the part is inside the folder but it doesn’t work.

	local PartsinShield = workspace:GetPartsInPart(Shield)
	
	for i,v in pairs(PartsinShield) do
		if table.find(Attacks,PartsinShield[i]) then
			print("true")
			sound:Play()
		end
	end

Any help is appreciated

2 Likes

Is there anything more to the script?

This is the entire code

local remote = game:GetService("ReplicatedStorage").ShieldEvent

local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local db = false
local Attacks = game:GetService("ReplicatedStorage").Attacks:GetChildren()
remote.OnServerEvent:Connect(function(plr)
	local Shield = game:GetService("ReplicatedStorage").Attacks.Shield:Clone()
	local sound = Shield:FindFirstChild("Touched")
	plr.Character.HumanoidRootPart.Anchored = true
	Shield.Position = plr.Character.HumanoidRootPart.Position
	Shield.Parent = workspace
	
	local Forcefield = Instance.new("ForceField")
	Forcefield.Parent = plr.Character
	Forcefield.Visible = false
	
	local PartsinShield = workspace:GetPartsInPart(Shield)
	
	for i,v in pairs(PartsinShield) do
		if table.find(Attacks,PartsinShield[i]) then
			print("true")
			sound:Play()
		end
	end
	
	Debris:AddItem(Shield,0.7)
	Debris:AddItem(Forcefield,0.7)
	wait(0.7)
	plr.Character.HumanoidRootPart.Anchored = false
end)

I am seeing that you are calling this one time not every time something may interact with the hitbox, Perhaps try connecting this to a touched event.

1 Like

You can check if a child of an instance exists by using Instance:FindFirstChild(Name).

Sorry in advance if I misunderstood the question.

If the Array is not Empty, that means that there is an object insde of it, so when Attacks touches the Sheild you can check the Instance using v in the for loop (v is the value of the index aka The Instance) and check whether its called Attacks or has a Property Related to Attacks

for _,item PartsinShield do -- "v" is called "item" here (Dont get confused)
    if item.Name == "Attacks" then
        print(true)
        sound:Play()
    else
        continue -- skips the item
    end
end

You are using table.find() very poorly here, it is used to help you find an object, Instead what you are doing here is looking for the object you already have found, by checking the index you are currently iterating through.

OverlapParams.

local overlayParams = OverlapParams.new()
overlayParams.FilterType = Enum.RaycastFilterType.Include
overlayParams.FilterDescendantsInstances = {workspace.Attacks}

local partsInShield = workspace:GetPartsInPart(Shield, overlayParams)

if partsInShield then
	sound:Play()
end
1 Like

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