Is there any other way other than detecting tick with a loop more than a given seconds

So I was making a combat system where you have a m1 combo and other stuff. I added a condition when player will get stun or burn or something. So when player got hit, they’ll be stunned for a second. I added task.wait(seconds) to complete the function, but the problem is they do overlapping. There’s only one way I can do is subtracting a current tick and unupdated tick and check if the differences is more than a second, then it will set stun to false. In order to do that. I need to use a loop for this, I think it’s little bit messy. I want to know if there are other methods I can do for this.

Condition ModuleScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClassifyModule = require(ReplicatedStorage.Modules.Classify)
local Data = require(ReplicatedStorage.Modules.Data)
local stunTick = {}

local module = {}
module.__index = module


function module.New(State,Condition,CharacterToSet : Model)
	
	local moduleParameters = {
		
		["State"] = State,
		["Condition"] = Condition,
		["CharacterToSet"] = CharacterToSet
		
		
	}
	
	function module:Stun(seconds : number)
		local Condition = self.Condition
		local CharacterToSet = self.CharacterToSet
		stunTick[CharacterToSet] = tick()
		ClassifyModule.SetValue(Condition.Condition.Stun,true)
		
		while task.wait() do -- This is where i want to change
			if tick() - stunTick[CharacterToSet] >= seconds then
				ClassifyModule.SetValue(Condition.Condition.Stun,false)
			end
		end
		
	end

	function module:BlockBreak(stun)
		if stun then module:Stun() end
	end
	
	
	return setmetatable(moduleParameters,module)
end


return module

Classify ModuleScript:

local module = {}

function module.GetSameStringForObject(thisValue : string?,from : Instance)
	for _,v in pairs(from:GetChildren()) do
		if thisValue == v.Name then
			return v
		end
	end
end

function module.SetValue(object:Instance,value:ValueBase)
	object.Value = value
end




return module

Any more method or solutions?

Have you tried using task.delay()?

I used that before. Does it overlap like the task.wait()?

It shouldn’t overlap if you check if the player is already stunned

I tested it out. It’s probably the same as the task.wait(). Like when first hit, then it will wait for the time given. The other hit doesn’t reset the delay until player isn’t stunning anymore.

That’s why I’m trying to use tick() for this. I want something that doesn’t include loop or something in a shorter code.

If you want to check the stun as often as you are, while being able to edit it at any time, all without using loops, the only other solution I can think of is runservice.
You can format your current code differently too so that it looks cleaner.

1 Like

It shouldn’t be the same as task.wait, task.delay(time, callback) schedules a function/coroutine to be run after the time given. For example:

task.delay(1, function() print("Does this prints first maybe") end)

print("Don't mind me if I...")

-- Output
-- Don't mind me if I...
-- After 1 second => Does this prints first maybe

More information about this:

1 Like