So through an event I use my SCP867Service:Test onto my Effect’s module
--Service script
function SCP867Service:Test(player)
Effect.Apply(self.PrintSomething, player, 10, 1, "Test")
wait(5)
Effect.Remove(self.PrintSomething, player, "Test")
end
--Module script
function effect.Apply(func, player, duration, tickSpeed, tag)
--Check if the player already has the effect, bad code we don't check nil cases
local owningPlayer = game.Players:GetPlayerFromCharacter(player.Parent)
if effectsStorage[owningPlayer] and not Macros.findValue(effectsStorage[owningPlayer], func) then
--Adding to all tables
local functionCoroutine = coroutine.create(function()func(player, duration, tickSpeed)end)
In debugging the values look correct, with the player being the humanoid I passed, duration being 10 and tickSpeed being 1. Which then executes a function in the original service script:
function SCP867Service:PrintSomething(player, duration, tickSpeed)
for i = 1, duration, 1 do
print("Testing count: ", i)
task.wait(tickSpeed)
end
end
However here player = 10, duration =1 and tickSpeed = nil
What causes this?
I did all debugging I could and found that somewhere between the module script and the function being executed in my coroutine an additional value was required. I added a patch work solution of passing an additional nil before my player to make my code work, but doesn’t seem like the proper solution.
local functionCoroutine = coroutine.create(function() func(nil, player, duration, tickSpeed)end)
gameking000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Let’s follow the path of func
inside this coroutine :
coroutine.create(function()
func(player, duration, tickSpeed)
end)
You pass it into the parent function as self.PrintSomething
but it is defined on the module as <moduleName>:PrintSomething
. Notice the difference between the period and the colon?
When you define a function using a colon, this is syntactic sugar for a function signature that passes in the self
object as a hidden first argument. What this means is this :
function module:PrintSomething(a, b, c) end
-- is the same as...
function module.PrintSomething(self, a, b, c) end
So, because you defined your PrintSomething
function with a colon, there is an expectation you will call the function with a colon, or manually pass in the self
object. Since you didn’t, it is treating the first argument you did pass in as the self
which is why all of your inputs are offset by one.
In this case, you’re lucky, since your PrintSomething
function doesn’t access self
anywhere, it doesn’t need to be a member function, you can make it a static one. So just change the function signature to be a period function :
function SCP867Service.PrintSomething(player, duration, tickSpeed)