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

Instance.new wont anchor?

Asked by 8 years ago
while true do
    Instance.new("Part", game.Workspace).Name = "Fred"
    game.Workspace.Fred.Anchored = true
    game.Workspace.Fred.CanCollide = false
    wait(1)
end

--> Whenever the part is inserted, it will not anchor, but that is only on a "while true do" loop, when I remove the loop, it anchors just fine, but it only makes one?

2 answers

Log in to vote
-1
Answered by
RedCombee 585 Moderation Voter
8 years ago

Because you're making a new part in the Workspace with the same name each time, the game will not know what part you are referencing.

This should fix the problem:

a = 1/0 -- Variable equal to infinity
for i = 1,a do -- A for loop that will go on forever, like the while
    part = Instance.new("Part",game.Workspace)
    part.Name = ("Part"..i)
    part.Anchored = true
    part.CanCollide = false
    wait(1)
end
0
I didn't downvote btw ScriptsAhoy 202 — 8y
Ad
Log in to vote
4
Answered by 8 years ago

That's because you used it's name. The script will find the name, then anchor the part.

Then hen the loop repeats, it'll find the name, the old one, and even though there are 2 of them, it will anchor the one it finds, the first one

while wait(1) do
   local part =  Instance.new("Part", game.Workspace)
part.Name = "Fred"
    part.Anchored = true
    part.CanCollide = false
end

You can use the Instance.new as a var, and that's how you would mess with it.

0
This is essentially the same as mine, but this one looks cleaner and his explanation is better. I suggest using this to comprehend instead. RedCombee 585 — 8y

Answer this question