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

How do I make the rest of my code run at the same time as my while true loop?

Asked by 7 years ago
Edited 7 years ago

I made some code in Filtering Enabled which requires a while loop needed to update a part's position every .1 second. But of course when I'm running the loop, the rest of the code doesn't run which is a problem. Help? I could make another script to loop it but the main script still needs the variable because its F.E.

This is what is in the script: (The while loop is commented)

01script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player,mouse,anim,Jailed,Jailed2,Hit,isCaught)
02    --local Jailed1 = Instance.new("Part",Player.Character)
03    --Jailed1.Transparency = 1
04    --Jailed1.Anchored = true
05    --Jailed1.CanCollide = false
06    --Jailed1.Name = ("Here")
07    --while true do
08        --Jailed1.CFrame = Player.Character.Torso.CFrame
09        --wait(.1)
10    print("hi")
11    local Jailed1 = Player.Character.Tool.Handle
12    local attachmentB = Instance.new("Attachment",Jailed2)
13    local attachmentA = Instance.new("Attachment",Jailed1)
14    attachmentA.Position = Vector3.new(1,-0.7,0.3)
15    attachmentB.Position = Vector3.new(0,0,0.3)
View all 38 lines...
0
Not that it matters with your particular question but this is terrible framework for work in FE as you've only set the event up to deal with one type of communication within the game. Check this video out it might help: https://www.youtube.com/watch?v=m_CsACgDwOM Pejorem 164 — 7y

1 answer

Log in to vote
0
Answered by
Pejorem 164
7 years ago
Edited 7 years ago

tfw code gives you cancer... but anyway here's two uses of a thing called coroutines that you can research on the roblox wiki.

Basic use (don't need to learn about coroutines apart from the fact they're asynchronous and run in their own thread as if it's like a Script inside a Script... bad explanation but who cares this is meant to be simple).

1spawn(function()
2    while true do
3        print("doing stuff")
4        wait()
5    end
6end)
7 
8print("Other stuff")

doing stuff Other stuff doing stuff doing stuff ....

Basic use of coroutines:

1local leCoroutine = coroutine.create(function()
2    while true do
3        print("doing stuff")
4        wait()
5    end
6end)
7 
8coroutine.resume(leCoroutine)
9print("Other stuff")

doing stuff Other stuff doing stuff doing stuff ...

0
Why did it remove the line breaks for the output e.e.e.e.e.e Pejorem 164 — 7y
Ad

Answer this question