Waiting in a task.spawn

hello everyone,

i’m currently making a Debuff system and i’m having trouble with making the Petrified one

what i want it to do, is disable all the abilities that the player has, and after the specified duration, re-enable them again

the disabling and re-activating are working perfectly, however the waiting for the re-enabling is kind of tripping me
this is the code for it

coroutine.create(function ()
			for _, abilities in pairs(PlayerData.CurrentAbilities) do
				PetrifiedEvent:FireClient(player, abilities)
				UI:LockAbility(player, abilities.Name)
				task.wait(0.01)
			end
			
			task.wait(duration)

			--task.spawn(function ()
				for _, abilities in pairs(PlayerData.CurrentAbilities) do
					Unpetrify:FireClient(player, abilities, PlayerData.Character)
					UI:UnlockAbility(player, abilities.Name)
					task.wait(0.01)
				end
			--end)
		end)

if i put a waiting function on the main DebuffModule, it’s just gonna add additional time and i don’t want that

any support helps, thanks

i’ve tried using task.spawn, coroutine.create, coroutine.wrap but nothing really seems to work

You need to say what the issue is when making topics asking for help. What’s not working correctly in the code you’ve sent?

His issue is that the wait time between the disabling and re-enabling of the abilities isn’t working properly.

however the waiting for the re-enabling is kind of tripping me

1 Like

What is your ‘duration’ variable set to? And why did you put it into a coroutine? Coroutine is usually used for loading stuff. Also i’m pretty sure you should also use coroutine.resume() if you’re gonna be using a coroutine.

the duration variable is set when running the method of the main Debuff module

Could you show me both entire scripts?
Is your coroutine function able to access said variable? Are you sure the variable’s value isn’t getting reset somehow?

let me try and use coroutine.resume
sorry i didn’t know that it was mainly used for loading

1 Like

the DebuffModule script

local UI = require(game:GetService("ReplicatedStorage").UI)
local GameDataManager = require(game:GetService("ReplicatedStorage").GameDataManager)
local Debuffs = require(script.Debuffs)

local module = {}

function module:RemoveDebuff(player: Player, name)
	local PlayerData = GameDataManager:GetPlayerData(player)
	
	if not PlayerData["Debuffs"][name] then return warn("player selected does not have debuff") end
	
	Debuffs[name](player, 0, 0)
	PlayerData["Debuffs"][name] = nil
	
	UI:RemoveDebuff(player, name)
end

function module:GiveDebuff(player: Player, name, amount, duration)
	local PlayerData = GameDataManager:GetPlayerData(player)
	local DebuffClientEvent = game:GetService("ReplicatedStorage").Events.UpdateDebuffClient
	
	PlayerData["Debuffs"][name] = {
		Amount = amount,
		Duration = duration
	}
	
	DebuffClientEvent:FireClient(player, PlayerData["Debuffs"])
	
	UI:CreateDebuff(player, name, amount, duration)
	Debuffs[name](player, amount, duration)
	
	--task.wait(duration)
	PlayerData["Debuffs"][name] = nil
	print(PlayerData["Debuffs"])
	Debuffs[name](player, 0, 0)
	
	DebuffClientEvent:FireClient(player, PlayerData["Debuffs"])
end

return module

the Debuffs module script, containing all the Debuffs needed for the main module

local GameDataManager = require(game:GetService("ReplicatedStorage").GameDataManager)

local module = {
	Slowness = function (player, amount, duration)
		local PlayerData = GameDataManager:GetPlayerData(player)
		
		local character = player.Character
		local humanoid = character.Humanoid
		humanoid.WalkSpeed = PlayerData["GameInfo"].OriginalWalkSpeed - amount
	end,
	Petrified = function(player, amount, duration)
		local UI = require(game:GetService("ReplicatedStorage").UI)
		local PlayerData = GameDataManager:GetPlayerData(player)
		local PetrifiedEvent = game:GetService("ReplicatedStorage").Events.Petrified
		local Unpetrify = game:GetService("ReplicatedStorage").Events.Unpetrify
		
		local thing = coroutine.create(function ()
			for _, abilities in pairs(PlayerData.CurrentAbilities) do
				PetrifiedEvent:FireClient(player, abilities)
				UI:LockAbility(player, abilities.Name)
				task.wait(0.01)
			end

			task.wait(duration)

			--task.spawn(function ()
			for _, abilities in pairs(PlayerData.CurrentAbilities) do
				Unpetrify:FireClient(player, abilities, PlayerData.Character)
				UI:UnlockAbility(player, abilities.Name)
				task.wait(0.01)
			end
			--end)
		end)
		
		coroutine.resume(thing)
	end,
}

return module

From what i’m seeing your duration variable is not being set to anything. All i can see concerning it is that you’re setting it to ‘0’ (i’m not sure if youre actually doing that but it seems like you are) at:
Line 35 of the DebuffModule script

Debuffs[name](player, 0, 0)

If it’s set to 0 then it’s not gonna wait, instead it should be something like
Debuffs[name](player, 0, 1)

By the way, you’re using Module scripts knowing that they need to be called out to run right? Just making sure.

yes
in fact, i’m testing the debuffs using CMDR
also, the duration of the debuff is being set at line 30 of the debuff module

What number value is it being set to though? “duration” is a variable, not a number value, and if you want it to work you need to set the duration variable to a number value.

it’s being set to any number
like 10, 120, 60, basically just any number

Is it being randomized? If so at what line? Because if it’s any number it can range from 0 to waiting hours.
Know that task.wait(1) = wait 1 second. So if it’s waiting for 120 it’s gonna be waiting for 2 minutes and so on.

it’s being set when a command using the CMDR bar is ran
this is the module script of the structure for the command

return {
	Name = "give-debuff",
	Description = "Gives a debuff to a player",
	Args = {
		{
			Type = "player",
			Name = "Player",
			Description = "The player affected with the debuff"
		},
		{
			Type = "debuff",
			Name = "Debuff",
			Description = "The debuff to give to the player"
		},
		{
			Type = "number",
			Name = "Amount",
			Description = "The amount of the debuff"
		},
		{
			Type = "number",
			Name = "Duration",
			Description = "The duration of the debuff"
		}
	}
}

and this is the server module script

return function (context, player, debuff, amount, duration)
	local DebuffModule = require(game:GetService("ReplicatedStorage").DebuffModule)
	
	DebuffModule:GiveDebuff(player, debuff, amount, duration)
end

And what’s triggering the debuff/cooldown?

the server module script of the command
which calls the method of the DebuffModule

I see well you should just get your first code out of the coroutine and instead having it be under a function connected to an event.

sorry if i couldn’t get back to you
recently, i have not been feeling good so i took a quick rest in bed
i’ll try to find any other solution