Help with my loop script

So… Im just started today to make scripts (ive seen alvinblox for 1 month 00f) And i made a loop script (The loop make a part and too a sparkle) now whats my problem?

Problem: I want to parent the sparkle to each part the loop create

Code:

--loop

while true do

--Part

part = Instance.new("Part")

part.Anchored = true

part.CanCollide = false

part.BrickColor = BrickColor.new("Bright orange")

part.Name = "Orange"

part.Parent = game.Workspace

part.Material = "Neon"

part.Locked = false

part.Reflectance = 1

--Sparkles

sparkles = Instance.new("Sparkles")

sparkles.Parent = game.Workspace.Orange

--Prints

print("Orange Created")

print("Sparkles Created")

wait(0.5)

end

please help me… I am begginer scripter :confused:

3 Likes

You index the Instance created of the new Part to the variable part, you can simply just change this line:

sparkles.Parent = game.Workspace.Orange

to:

sparkles.Parent = part

Also, you should set all of your variables created inside other threads to local. So like this:

local part = Instance.new("Part")

Hope this helps, and good luck on your path to learn how to script!

5 Likes

If you want to parent the sparkles to the part each time. You can use the second argument in the Instance.new() function to do so.

For Example

local T = Instance.new("Part", workspace)
local Exp = Instance.new("Explosion", T) --Sets the Explosion's parent to the created part.
1 Like

(seeing as it’s being used in a while true do loop it would be especially bad to use it)

5 Likes

When posting code to the DevForum, please include your code in a code block. You can accomplish this by using three back ticks and posting your code between them.

```lua
– Your code
```


Just a note that you’d probably want to create the assets first before setting their parents.

while true do
    local part do
        part = Instance.new("Part")
        part.Name = "Orange" -- And all your other properties, EXCEPT PARENT
    end
    local sparkles do
        sparkles = Instance.new("Sparkles")
    end
    sparkles.Parent = part
    part.Parent = workspace.Orange
    wait(0.5)
end

It’s always good practice if you set properties first before the parent, so you can avoid any odd issues arising out of the cause of what order you parent things in. You should also parent children first before the actual part itself (i.e. the sparkles).

If you want to learn more about about posting code blocks/snippets, please read this post

Inserting Code Snippets

1 Like