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

The Mesh and ParticleEmitter Properties won't Change?

Asked by 9 years ago

I've made a script, but none of the properties will change. No errors show up on the output.

p = Instance.new("Part", game.Workspace)
pe = Instance.new("ParticleEmitter", p)
m = Instance.new("SpecialMesh", p)

---Part---
p.Position = Vector3.new(-43.2, 2.494, -61.4)
p.Locked = "True"
p.Anchored = "True"
p.TopSurface = "Smooth"
p.BottomSurface = "Smooth"
while true do
p.BrickColor = BrickColor.random()
wait(0.5)
end

---ParticleEmitter---
pe.Lifetime = NumberRange.new(.5, 5)
pe.Speed = 5
pe.Size = NumberSequence.new(.2)

---Mesh---
m.MeshId = "http://www.roblox.com/asset/?id=21057410"
m.TextureId = "http://www.roblox.com/asset/?id=21243140"

1 answer

Log in to vote
1
Answered by 9 years ago

The problem is that you have an infinite while loop right before the code that edits the properties of the Mesh and the ParticleEmitter.

Programs are read from the top-down, line by line. When the program gets to the while loop, it keeps on repeating the loop until it terminates, then it goes onto the next lines of code.

The problem is that your loop never terminates, so the program never goes past the loop onto the next lines of code. The solution to that is to have the loop run as a separate process away from the main program. By doing so, you can have the loop running and the program will run whatever code is underneath it.

There are a few methods of doing that, one of which is the Coroutine. The method that I'm going to be using is far simpler, the Spawn() function.

Basically, the Spawn() function is used to run a function as a separate process away from the main program, like so:

print("Line 1")
spawn(function()
    while true do
        print("This is looping!")
        wait(1)
    end
end)
print("Line 2")

What you'll notice if you run that code is that the line after the while loop gets printed and the line inside the while loop itself get printed too. Amazing right?

To fix your code, we just need to put the while loop in a spawn() function, like so:

p = Instance.new("Part", game.Workspace)
pe = Instance.new("ParticleEmitter", p)
m = Instance.new("SpecialMesh", p)

---Part---
p.Position = Vector3.new(-43.2, 2.494, -61.4)
p.Locked = "True"
p.Anchored = "True"
p.TopSurface = "Smooth"
p.BottomSurface = "Smooth"
spawn(function()
    while true do
        p.BrickColor = BrickColor.random()
        wait(0.5)
    end
end)

---ParticleEmitter---
pe.Lifetime = NumberRange.new(.5, 5)
pe.Speed = 5
pe.Size = NumberSequence.new(.2)

---Mesh---
m.MeshId = "http://www.roblox.com/asset/?id=21057410"
m.TextureId = "http://www.roblox.com/asset/?id=21243140"

Hope this helped!

0
Ah, thank you! I've just started getting into loops and stuff. I've been playing around with scripts and this is what I came up with. I appreciate your help. SimplyRekt 413 — 9y
Ad

Answer this question