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)

script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player,mouse,anim,Jailed,Jailed2,Hit,isCaught)
    --local Jailed1 = Instance.new("Part",Player.Character)
    --Jailed1.Transparency = 1
    --Jailed1.Anchored = true
    --Jailed1.CanCollide = false
    --Jailed1.Name = ("Here")
    --while true do
        --Jailed1.CFrame = Player.Character.Torso.CFrame
        --wait(.1)
    print("hi")
    local Jailed1 = Player.Character.Tool.Handle
    local attachmentB = Instance.new("Attachment",Jailed2)
    local attachmentA = Instance.new("Attachment",Jailed1)
    attachmentA.Position = Vector3.new(1,-0.7,0.3)
    attachmentB.Position = Vector3.new(0,0,0.3)

    print("hi")
    local ropeConstraint = Instance.new("RopeConstraint",Jailed1)
    ropeConstraint.Attachment0 = attachmentA
    ropeConstraint.Attachment1 = attachmentB
    ropeConstraint.Length = 10
    ropeConstraint.Visible = true
    ropeConstraint.Color = BrickColor.Black()
    ropeConstraint.Thickness = 0.2
    ropeConstraint.Restitution = 0.5
    local caught = Jailed.Humanoid:LoadAnimation(anim)
    caught:Play()


    script.Parent.Unequipped:Connect(function() 
        ropeConstraint:Destroy()
        attachmentA:Destroy()
        attachmentB:Destroy()
        caught:Stop()
        isCaught = false
    end)

end)
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).

spawn(function()
    while true do
        print("doing stuff")
        wait()
    end
end)

print("Other stuff")

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

Basic use of coroutines:

local leCoroutine = coroutine.create(function()
    while true do
        print("doing stuff")
        wait()
    end
end)

coroutine.resume(leCoroutine)
print("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