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

Ending this Function and making it play in the background?

Asked by 7 years ago

Hey, i made a tool. When clicked, it clones a minion and is parented to workspace. It moves and follow the player. I want to make the while true do in the background, as it cannot finish the function until the while true do is stopped. I also want to do this so i could 'summon' more minions. I tried making a 'return false' but it would be an error. Is there any other ways to do this?

function MinionControl(MinionModel)
    MinionModel.PrimaryPart = MinionModel:FindFirstChild("CenterPart")
    wait(0.1)
    MinionModel:SetPrimaryPartCFrame(tool.Parent.Torso.CFrame)
    local bodyposition = Instance.new("BodyPosition")
    bodyposition.Parent = MinionModel:FindFirstChild("CenterPart")
    bodyposition.MaxForce = Vector3.new(500000000, 500000000, 500000000)
    while true do
        bodyposition.position = tool.Parent.Torso.Position + Vector3.new(math.random(-range.Value, range.Value), math.random(0, 6), math.random(-range.Value, range.Value))
        MinionModel:FindFirstChild("CenterPart").CFrame = CFrame.new(MinionModel:FindFirstChild("CenterPart").CFrame.p, tool.Parent:FindFirstChild("Torso").CFrame.p)       
        wait(0.5)
    end
end 

tool.Equipped:connect(function()
    tool.Activated:connect(function()
        if enabled == false and tool.Parent:FindFirstChild("Humanoid") and tool.Parent:FindFirstChild("Humanoid").Health  > 0 then
            enabled = true
            local cloneminion = minion:Clone()
            cloneminion.Name = tool.Parent.Name
            cloneminion.Parent = workspace
            MinionControl(cloneminion)
            wait(cooldown.Value)
            enabled = false

        end
    end)
end)
0
use a spawn function xuefei123 214 — 7y
0
^ (For line 22, more specifically.) TheeDeathCaster 2368 — 7y

1 answer

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

*If you put a wait() in your script, the whole script will pause until the wait time is over.

To make a script run in the background, (especially when the script is pause during wait time) make a coroutine (using coroutine.resume).

*Also, read this, this is my source. *

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

Ad

Answer this question