Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is it possible for another part of a script run while you have a for loop?

This script crashes studio it does not even run :(

coroutine.resume(coroutine.create(function()
while true do
MagicSym = Instance.new("Part")
	MagicSym.Parent = me.Character.Torso
	MagicSym.Anchored = true
    MagicSym.CanCollide = false
    MagicSym.formFactor = "Symmetric"
    MagicSym.BrickColor = BrickColor.new("New Yeller")
    MagicSym.Transparency = 1
    MagicSym.Name = "MagicSym"
    MagicSym.Size = Vector3.new(30, 1, 30)
    MagicSym.TopSurface = "Smooth"
    MagicSym.BottomSurface = "Smooth"
    MagicSym.CFrame = me.Character.Torso.CFrame*CFrame.new(0,-3, 0)
	
	
	d = Instance.new("Decal")
	d.Name = "Circle"
	d.Parent = MagicSym
	d.Face = "Top"
	d.Texture = "http://www.roblox.com/asset/?id=214848393"
	p = Instance.new("Decal")
	p.Name = "Circle"
	p.Parent = MagicSym
	p.Face = "Bottom"
	p.Texture = "http://www.roblox.com/asset/?id=214848393"
end
end
)
)
while true do
	for loop = 1, 50 do
    MagicSym.CFrame = MagicSym.CFrame *CFrame.Angles(0, 0.005, 0)
    wait(1/25)
wait()
end


end

This is what I made of it. The only problem is the crashing

2 answers

Coroutines.

http://wiki.roblox.com/index.php?title=Coroutines

coroutines.resume(coroutine.create(function()
while true do
print("looping with each other")
wait()
end
end))
while true do
print("its just one script")
wait()
end

Reminder. DON'T DO THIS..

script.Parent.Touched:connect(function()
coroutines.resume(coroutine.create(function()
while true do
print("looping with each other")
wait()
end
end))
end)

You will lag the server because when you create a co-routine, it's creating another thread. Too many thread's will degrade server performance. You'll need to ask another question on how to kill co-routines.

2 comments

0

Yeah I remember doing the same mistake a while back, don't do it

0

Although the game runs perfectly fine, any server sided script is slower with that mistake.

--If you mean you want the script to run with a infinite loop effectively then add a 
--term from Rbx.Lua called break****
--Or if you mean just run it with no infinite loops just add a wait() in between while true do wait()
coroutine.resume(coroutine.create(function()
while true do
MagicSym = Instance.new("Part")
    MagicSym.Parent = me.Character.Torso
    MagicSym.Anchored = true
    MagicSym.CanCollide = false
    MagicSym.formFactor = "Symmetric"
    MagicSym.BrickColor = BrickColor.new("New Yeller")
    MagicSym.Transparency = 1
    MagicSym.Name = "MagicSym"
    MagicSym.Size = Vector3.new(30, 1, 30)
    MagicSym.TopSurface = "Smooth"
    MagicSym.BottomSurface = "Smooth"
    MagicSym.CFrame = me.Character.Torso.CFrame*CFrame.new(0,-3, 0)


    d = Instance.new("Decal")
    d.Name = "Circle"
    d.Parent = MagicSym
    d.Face = "Top"
    d.Texture = "http://www.roblox.com/asset/?id=214848393"
    p = Instance.new("Decal")
    p.Name = "Circle"
    p.Parent = MagicSym
    p.Face = "Bottom"
    p.Texture = "http://www.roblox.com/asset/?id=214848393"
--A break here--The break word should make it render a bit between the script so that the script can run with the infinite loop.
break 
end
end
)
)
while true do
    for loop = 1, 50 do
    MagicSym.CFrame = MagicSym.CFrame *CFrame.Angles(0, 0.005, do--So we do break between the waits
    wait(1/25)
break
wait()
end


end --If this helped leave an upvote or an Accept much appreciated.

This discussion is preserved as it was. New answers, comments, and votes are closed.