Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with "in pairs"?

Asked by
Qorm 100
9 years ago
door=game.Workspace.trigger1.buttonpart
local guy=game.Workspace.hangingchain.guy

function onTouch()
    door:remove()
    for i,v in pairs(guy) do
        guy[i]remove()
    end
end

script.Parent.Touched:connect(onTouch)

Output: Workspace.opentrigger1.trigger.Script:6: bad argument #1 to 'pairs' (table expected, got userdata)

1 answer

Log in to vote
1
Answered by 9 years ago

A for loop gets the amount of something within a table, for the function, you are not getting a table of any sort, just the Part/Model/Ect., also, the remove method, as stated on the ROBLOX WIKI, the remove method only reverts a Child's Parent to nil, I would use the Destroy method instead, last but not least, you are not checking if the specifier door and guy are exactly existant, please use the WaitForChild method or FindFirstChild method to check if existant. Here is how your script would look like;

local door=game.Workspace:WaitForChild("trigger1"):FindFirstChild("buttonpart") --This will wait until 'trigger1' has loaded, and finds 'buttonpart' within 'trigger1'
--What 'FindFirstChild' will do is if it finds that Child, it will return the Child, but if not it'll return the Child as 'nil'
local guy=game.Workspace:WaitForChild("hangingchain"):FindFirstChild("guy") --This will wait until 'hangingchair' has loaded, and finds 'guy' within 'hangingchair'

function onTouch() --Your function
if not door or not guy then return end --If 'door' or 'guy' is not existant then it will return, or, will not run the rest of the code
door:Destroy() --Destroys the door; The 'Destroy' method basically Destroys, or reverts the Child's Parent to nil, like 'remove', but only Locks it from being used again
for i,v in pairs(guy:GetChildren()) do --This will get a table of Child within 'guy'; the amount of Children within 'guy'
if v and v:IsA("BasePart") then --A checker so that there are no errors caused when looping; this will check to see if the Children within 'guy' are a 'Part' type instance (Part, WedgePart, CornerPart, ect.)
v:Destroy() --Before was wrong, because you called 'remove' wrong, and did not write the code correctly, 'v' is specified as the current Child within 'guy'
end --Ends the code for the 'if' statement
end --Ends the code for the 'for' loop
end --Ends the code for the function

script.Parent.Touched:connect(onTouch) --Connects the event to the function; whenever the Part is touched, it will fire the function 'onTouch'

Hope this helped!

Ad

Answer this question