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

Two objects are switching positions?

Asked by 8 years ago

For some reason the when the script below runs, my dropper model and my door part switch places. I can't figure out why for the life of me. The door shouldn't even be able to get or use the dropper position. The objects I'm cloning are in the same exact hierarchy as the original plot, so I don't see that being a reason. Any insight as to how this works would be greatly appreciated.

game.Players.PlayerRemoving:connect(function(player)
    if(player == Owner) then
        local doorClone = game.workspace.cloneParts.GetOwner:Clone()
        doorClone.Parent = script.Parent.Parent.Parent

        for index, child in ipairs(script.Parent.Parent:GetChildren()) do
            childOrig[index] = child

            if (childOrig[index]:IsA("Part")) then
                childOrigPos[index] = childOrig[index].Position

            elseif (childOrig[index]:IsA("Model")) then
                childOrigPos[index] = childOrig[index]:GetModelCFrame().p
            end
            childOrig[index]:Destroy()
        end

        for index, cloned in ipairs(doorClone:GetChildren()) do
            childClone[index] = cloned

            if (childClone[index]:IsA("Part")) then
                childClone[index].Position = childOrigPos[index]

            elseif (childClone[index]:IsA("Model")) then
                childClone[index]:MoveTo(childOrigPos[index])
            end
        end
        GetOwner:Destroy()
        print('cloned')
    end
end)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 8 years ago

:GetChildren() returns objects in no particular order. You should never use the index that you get from iterating over it.

Instead, use the child as the index for the original position

Code cleanup

It's not idiomatic to use () in conditions in an if.

You shouldn't use childOrig[index] to refer to child

You shouldn't use childClone[index] to refer to cloned


You're trying to compare objects between two different models. That's difficult to manage correctly (what if the models were completely different models?) Your code can't easily enforce that they're the same.

Really, you should copy from the representation that you created; by iterating over childOrig and creating new children and putting into a model (rather than cloning a model and trying to find the corresponding things in the map)

0
i thought setting the table at index to equal child would rectify that, but i'll try it out syntaxjedi 22 — 8y
0
oh wait, i see what you're saying, nevermind syntaxjedi 22 — 8y
0
I get an error when I switch out index with child, and I also noticed this only happens when I start searching for a model. When I take out the dropper model and add in a bunch of random parts they always spawn that the correct locations. syntaxjedi 22 — 8y
0
I'll look at this more carefully to see if there's something else BlueTaslem 18071 — 8y
View all comments (4 more)
0
thanks a bunch syntaxjedi 22 — 8y
0
Looking at it, I don't know how to rewrite it without a lot more information. I wrote my thoughts on what a solution might look like BlueTaslem 18071 — 8y
0
it doesn't seem very efficient to go through and index every child of every model, duplicate it, move it, and put it back into a model syntaxjedi 22 — 8y
0
I ended up going the wrong direction with it, there was no reason to index both original and cloned. All I had to do was index the clone and compare that to the original with (FindFirstChild:childClone[index].Name). That way I can get the position, delete the original, and spawn the clone at that position. syntaxjedi 22 — 8y
Ad

Answer this question