a splitter script where when touched will destroy any welds in the part that touched it
01 | return function () |
02 | script.Parent.Touched:Connect( function (t) |
03 | if t.Name = = "Pushbox" and t.Parent.Name or t.Parent.Parent.Name = = "DualityPushboxSpawnButton" then --seeing if the part touched is named 'pushbox and the parent's name of the pushbox is 'DualityPushboxSpawnButton' |
04 | for i,v in pairs (t.Parent or t.Parent.Parent) do |
05 | if v.Name = = "WeldConstraint" or "Weld" then |
06 | v:Destroy() -- seeing if the dualitypushboxspawnbutton contains any welds, and destroys them |
07 | end |
08 | end |
09 | end |
10 | end ) |
11 | end |
Image of what will be touching the splitter
https://gyazo.com/75e3ba528847c1466eb4d6aa01c2265f
Image of error
https://gyazo.com/1f04231c9332631f14af8510b167c171
The problem is that you're directly referencing the Object without calling GetChildren()
. The pairs function expects you to input a table (which is what GetChildren()
returns) but you're instead inputting an instance object (t.Parent
and t.Parent.Parent
), hence the error.
Fixing this is quite easy.
01 | return function () |
02 | script.Parent.Touched:Connect( function (t) |
03 | if t.Name = = "Pushbox" and t.Parent.Name or t.Parent.Parent.Name = = "DualityPushboxSpawnButton" then |
04 | for i,v in pairs (t.Parent:GetChildren() or t.Parent.Parent:GetChildren()) do -- Now the script uses the object's children instead of the object directly. |
05 | if v.Name = = "WeldConstraint" or "Weld" then |
06 | v:Destroy() destroys them |
07 | end |
08 | end |
09 | end |
10 | end ) |
11 | end |