How to return something in a module script

I’ve answered my own other question using a questionable method, but now.
I’m making 2 different functions in modules

local Stun = {}
function Stun.stun(hum, dur, dmg)
	hum:SetAttribute("Stunned", true)
	hum.WalkSpeed = 0
	hum:TakeDamage(dmg)
	wait(dur)
	hum.WalkSpeed = 16
	hum:SetAttribute("Stunned", true)
end

function Stun.stunned(hum, canhit)
	if hum:GetAttribute("Stunned") == true then
		canhit = false
		wait(dur)
	end
end
return Stun

I wanna know how to access dur from the other module, is that possible?

What you mean by “access” the variable “dur”?

In that Stun module, dur is not declared, you cant access it, dur is a parameter that you sent to that module from a different script.
You are using dur to make a wait(dur) in order to keep the player “stunned” for certain seconds, the seconds got from that variable. And once you run this line wait(dur), you cant change the time that the script gonna wait, so if you sent to the module function
Stun.stun(hum, 10, dmg). Once the script got that, dur will make a wait(10), you cant change it on the fly

return dur


local dur = Stun.stun(a, 1, b) → 1

If you have the “dur” variable stored in another module script, then you’ll have to require that other module script inside of this one that you’re currently using, then get the duration that way.

no not that, I was trying to say

how do i get dur from the upper function
to the bottom function
so could tell how long the person who was hit was stunned and correctly disable hitting for that amount of time

You could do it like this:

(but I think you should re-think the logic of your system, whats the purpose of having those 2 functions? stun() and stunned() when will be called? from what player interaction? and maybe instead of getting the variable from a different function, you could store the dur as an Attribute too, so the stunned() function checks the attribute in the Humanoid)

local Stun = {}

Stun.Duration = 5 -- variable holder

function Stun.stun(hum, dur, dmg)
	if dur then
		Stun.Duration = dur -- change/set the variable in the module
	end
	
	hum:SetAttribute("Stunned", true)
	hum.WalkSpeed = 0
	hum:TakeDamage(dmg)
	wait(dur)
	hum.WalkSpeed = 16
	hum:SetAttribute("Stunned", true)
end

function Stun.stunned(hum, canhit)
	if hum:GetAttribute("Stunned") == true then
		canhit = false
		task.wait(Stun.Duration) -- the dur
	end
end

return Stun
1 Like

i’ve found another way to do it but thanks for the tip i’ll be using this later if needed