How do I play 2 loops at the same time?

Im trying to play both of theese loops at the same time how would I do that?



local UIS = game:GetService("UserInputService")
local char = script.Parent

local keybind = Enum.KeyCode.E -
local canslide = true

local function iod() -Loop 1
	local value = 0

	while true do
		wait(0.1)
		game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Visible = true
		value += 0.09
		game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Position += UDim2.new(0,0,0.0045,0)
		game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.TextLabel.Visible =  true

		local newvalue =  math.round(value / 100) * 100
		game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.TextLabel.Text = string.format("%0.2f", 20 - value)
		if game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Position.Y.Scale >= 1   then
			game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Visible = false
			game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Position = UDim2.new(0,0,0,0)
			game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.TextLabel.Visible =  false
			break
		end
	end
end

UIS.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed then return end
	if not canslide then return end
	if  game.Players.LocalPlayer.folder.See.Value == false then return end
	if input.KeyCode == keybind then
		print("east")
		canslide = false
			iod()
			for i,v in pairs(game.Players:GetDescendants()) do  -Loop 2
				if v:IsA("Player") then
					local highlightnew = game.ReplicatedStorage.Highlight:Clone()
					highlightnew.Parent = v.Character
				end
			end
			task.wait(3)
			for i,v in pairs(game.Players:GetDescendants()) do
				if v:IsA("Player") then
					v.Character.Highlight:Destroy()
				end
			end
			
			
		canslide = true
	end
end)





Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Put your loops in a coroutine.wrap
here i edited the script

local UIS = game:GetService("UserInputService")
local char = script.Parent

local keybind = Enum.KeyCode.E
local canslide = true

local function iod() -- Loop 1
    local value = 0

    while true do
        wait(0.1)
        game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Visible = true
        value += 0.09
        game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Position += UDim2.new(0,0,0.0045,0)
        game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.TextLabel.Visible =  true

        local newvalue =  math.round(value / 100) * 100
        game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.TextLabel.Text = string.format("%0.2f", 20 - value)
        if game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Position.Y.Scale >= 1 then
            game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Visible = false
            game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.Frame.Position = UDim2.new(0,0,0,0)
            game.Players.LocalPlayer.PlayerGui.Stuff.Ability.See.CanvasGroup.TextLabel.Visible =  false
            break
        end
    end
end

local function iod2() -- Loop 2
    for i,v in pairs(game.Players:GetDescendants()) do
        if v:IsA("Player") then
            local highlightnew = game.ReplicatedStorage.Highlight:Clone()
            highlightnew.Parent = v.Character
        end
    end
    task.wait(3)
    for i,v in pairs(game.Players:GetDescendants()) do
        if v:IsA("Player") then
            v.Character.Highlight:Destroy()
        end
    end
end

UIS.InputBegan:Connect(function(input, gameprocessed)
    if gameprocessed then return end
    if not canslide then return end
    if game.Players.LocalPlayer.folder.See.Value == false then return end
    if input.KeyCode == keybind then
        print("east")
        canslide = false

        coroutine.wrap(iod)()
        coroutine.wrap(iod2)()

        canslide = true
    end
end)
2 Likes

Like @VonsRevenges said, use couroutine.wrap() or task.spawn()

but in my opinion couroutine.wrap() is slightly better.

1 Like

Use coroutines like @VonsRevenges said, they can run parallel.

Example:

--Example Arrays
local array1 = {
	1,
	2,
	5,
	20
}

local array2 = {
	5,
	25,
	125,
	0,
	1
}

--Setting the thread up
local thread = coroutine.create(function()
	--Array 1 loop
	for index, number in array1 do
		--Do whatever you want
	end
	
	--Array 2 loop
	for index, number in array2 do
		--Do whatever you want
	end
end)

--Run the coroutine
coroutine.resume(thread)```
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.