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

Parts disappearing after being cloned?

Asked by 4 years ago

Hi, I have a script that will distribute parts over my map but after they have spawned in and dropped to the ground they will just be removed. I can't figure out why this is happening.

This is the script that clones the part and drops the cloned parts across the map:

for original = 1, 75 do

    local pos1 = math.random(-1024, 1024)
    local pos2 = math.random(-1024, 1024)

    local original = game.Workspace.Health
    local copies = original:Clone()
    copies.Parent = original.Parent
    copies.Position = Vector3.new(pos1, 50, pos2)
    copies.Anchored = false

end

Any and all help will be appreciated!

0
I found out that it was actually a script inside the parts that was causing them to be deleted, but all of the answers on this were also very helpful, thank you! Cyvalis 2 — 4y

2 answers

Log in to vote
0
Answered by
SmartNode 383 Moderation Voter
4 years ago

That is because Anchored is set to false. Please re-enable it and let me know how it goes.

0
Seems to be working now, thank you. Do you know if there's any way I could have them not be anchored that wont break them? Cyvalis 2 — 4y
0
If only the cloned ones are breaking, just stop setting the anchored property completely. When you :Clone() an object you copy the name and properties altogether so it should not break as long as the original doesn’t break. SmartNode 383 — 4y
0
I see, thanks again! Cyvalis 2 — 4y
0
No problem, happy to help! SmartNode 383 — 4y
Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago

You are using variable original both as a indices and health item reference. You should never use variables with the same name, as it may cause some really weird bugs. Please change your code like this:

local original = game.Workspace.Health -- that line can be outside loop

for i = 1, 75 do

    local pos1 = math.random(-1024, 1024)
    local pos2 = math.random(-1024, 1024)

    local copies = original:Clone()
    copies.Parent = original.Parent
    copies.Position = Vector3.new(pos1, 50, pos2)
    copies.Anchored = false

end

As SmartNode mentioned, if your health item is non collidable, it will fall through and get destroyed by engine. Make sure CanCollide is set to true for your items.

Answer this question