Are modules supposed to run functions inside of the script they're called in?

  1. What do you want to achieve? A module script that handles debounces

  2. What is the issue? when I call the debounce in the script after the ability is finished, the script i called it in waits the amount of seconds the cooldown lasts before continuing with the rest of the code.

  3. What solutions have you tried so far? I’ve tried reading about module scripts but they provided me with no insight and other posts didn’t give me answers I was looking for.

here’s an example script (server)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local Remote = ReplicatedStorage.Events:FindFirstChild("Spell")
local Effects = require(ReplicatedStorage.Modules.Effects)
local DebounceSystem = require(ServerScriptService.Modules.DebounceSystem)

Remote.OnServerEvent:Connect(function(player, SpellName, hitPart, worldPosition, Cooldown)
	if SpellName == "Incendia" then
		if hitPart.Parent:FindFirstChildOfClass("Humanoid") then
			DebounceSystem:AbilityBegun(player, SpellName)
			local plrCharacter = player.Character
			local targetCharacter = hitPart.Parent
			
			Effects:WitchEffect(player)
			
			for i, v in pairs(targetCharacter:GetDescendants()) do
				if v:IsA("BasePart") or v:IsA("MeshPart") and v.Parent.Name ~= "Clothes" then
					for i, particle in pairs(ReplicatedStorage.Effects.PlayerFire:Clone():GetDescendants()) do
						if particle:IsA("ParticleEmitter") then
							local cloneParticle = particle:Clone()
							cloneParticle.Parent = v
							task.wait()
						end
					end
				end
			end
			DebounceSystem:AbilityEnded(player, SpellName, Cooldown) -- waits 5 seconds (the cooldown amount) before continuing to damage the target and etc which is what im trying to get rid of.
			targetCharacter:SetAttribute("Burning", true)

			for i = 1,10 do
				if targetCharacter:GetAttribute("Burning") == true then
					wait(0.5)
					targetCharacter.Humanoid:TakeDamage(3)
				else
					print("cant burn me bitch")
				end
			end
			
			targetCharacter:SetAttribute("Burning", false)

			for i, v in pairs(targetCharacter:GetDescendants()) do
				if v.Name == "Fire" then
					v.Enabled = false
					task.wait()
				end
			end
						
			for i, v in pairs(targetCharacter:GetDescendants()) do
				if v.Name == "Fire" then
					v:Destroy()
				end
			end
		end
	end
end)

here’s the debounce module script:

local DebounceSystem = {}

function DebounceSystem:AbilityBegun(player, SkillName)	
	player.Character:SetAttribute(SkillName.."Debounce", true)
	player.Character:SetAttribute("CanAttack", false)
end

function DebounceSystem:AbilityEnded(player, SkillName, Cooldown)
	player.Character:SetAttribute("CanAttack", true)
	wait(Cooldown)
	player.Character:SetAttribute(SkillName.."Debounce", false)
end

return DebounceSystem


I believe the solution to your problem would be running the function inside a separate thread:

function DebounceSystem:AbilityEnded(player, SkillName, Cooldown)
    task.spawn(function()
	    player.Character:SetAttribute("CanAttack", true)
	    task.wait(Cooldown)
	    player.Character:SetAttribute(SkillName.."Debounce", false)
    end)
end

This allows both threads for the scripts to run in sync without interrupting each other

3 Likes