I am trying to analyze one of the scripts in the ModuleScript section of the Roblox wiki. However, after looking at this for a while, I cannot seem to grasp on why you need to return the function... I understand everything else perfectly. The 'func' variable would just be a function of a touch script which requires the debounce (second script)
Can someone also care to explain how I can use this in other situations so I can learn about module scripts?
ModuleScript
function debounce(func)
local running = false
return function(...) -- <----- explain this!!
if not running then
running = true
func(...) -- call the function used in the touch script
running = false -- after the "wait 3" in touch script finishes the function ends and running = false."
end
end
end
return debounce
TouchScript
debounce = require(game.ServerStorage.Debounce)
script.Parent.Touched:connect(debounce(function(hit)
if hit and game.Players:GetPlayerFromCharacter(hit.Parent) and hit.Parent.Humanoid.Health > 0 then
print('touched')
wait(math.random(3,5))
local explosion = Instance.new("Explosion", workspace)
explosion.Position = script.Parent.Position
explosion.Parent = workspace
script.Parent:Destroy()
end
end))

1 comment
Thanks!