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
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)
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.
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