Coroutine only works once

Hello there! I am currently trying to figure out the issues to my problem, but I cant wrap my head around it.

repeat wait() until game.Players.LocalPlayer.Character

local plr   	 = game.Players.LocalPlayer
local char  	 = plr.Character or plr.CharacterAdded:Wait()
local hum   	 = char.Humanoid
local hrp   	 = char:WaitForChild("HumanoidRootPart")

local effects = {}

local RepStorage = game:GetService("ReplicatedStorage")
local ParticlesFolder = RepStorage:WaitForChild("Particles")

local shockwave = ParticlesFolder.shockwave

local groundShockwave = coroutine.create(function()
	local clone = shockwave:Clone()
	clone.Parent = hrp

	clone:Emit(30)
    wait(2)
    clone:Destroy()
end)

function effects.Shockwave()
	coroutine.resume(groundShockwave)
end

return effects

It is in a module script, whenever I call the function, it works for the first time it gets called, then it just breaks.
Any help is appreciated!

Just use task.spawn

local function SpawnFunc(text)
      print(text)
end


task.spawn(SpawnFunction, text)

As shown on the coroutine | Roblox Creator Documentation page, you can use a while loop to accomplish this. Here’s the example they provided:

-- This function repeats a word every time its coroutine is resumed
local function repeatThis(word)
	local repetition = ""
	while true do
		-- Do one repetition then yield the result
		repetition = repetition .. word
		coroutine.yield(repetition)
	end
end

Then you can use either coroutine.create or coroutine.wrap to create the thread. Again, using the example code they provided, here’s an example of using coroutine.create with the above function:

local repetitionCoro = coroutine.create(repeatThis)
print(coroutine.resume(repetitionCoro, "Hello"))  -- true, Hello
print(coroutine.resume(repetitionCoro))           -- true, HelloHello
print(coroutine.resume(repetitionCoro))           -- true, HelloHelloHello

And here’s an example of using it with coroutine.wrap:

local f = coroutine.wrap(repeatThis)
print(f("Hello"))  -- Hello
print(f())         -- HelloHello
print(f())         -- HelloHelloHello
1 Like