#module script function has different function values than what was inputted

20 messages · Page 1 of 1 (latest)

dawn wasp
#

Ok so I have a module script with a function. This function has 2 passed in variables, a Vector3 and an Instance (which in my case, is a players' model.) For some reason, when I use a normal script to call this function, passing in a Vector3 first then an Instance, it gives me an error in the module script telling me that you can't add my vector3 value partPosition and Vector3.new(0, 5, 0) together because partPosition, even though I passed it in as a Vector3, is an Instance.

dawn wasp
#

oh yeah

#

sorry i forgot

#
-- Module script
local module = {}

function module:SpawnKillbrickAbovePlayer(partPosition, character)
  partPosition += Vector3.new(0, 5, 0)

  -- some other code that I'm not going to list here because it is irrelavent and too long
end

return module
-- Server script
local spawnBrick = require(game.ServerScriptService.SpawnBrick)

-- some code to randomly pick a player based upon a bunch of conditions that is also too long to put here
spawnBrick.SpawnKillbrickAbovePlayer(playersPosition, character) -- playersPosition is a Vector3, while character is an Instance/Model
odd jewel
dawn wasp
#

oops

#

works now

odd jewel
#

By using : in your function signature, you've told Lua(u) that the related function will subscribe to the implicit parameter "self", which consumes the first argument as its value. Since playersPosition is the first argument, character falls into the place of partPosition

dawn wasp
#

ohhh

#

makes sense

#

so just use periods instead?

odd jewel
#

For this case, it's more appropriate to define your functions with a period, yes

#

. is typically reserved for library functions, while : is used in pseudo OOP

#

Whatever syntax you use, must be used when you call the function

#

So

spawnBrick:SpawnKillbrickAbovePlayer(playersPosition, character)

Would also work

dawn wasp
#

yeah but whats better

odd jewel
#

The reason being is that

Table:Function(...)

Is syntax sugar for

Table.Function(Table, ...)

Here, the first argument is overridden as the origin of the function call, and the passed arguments are offset by one. This is what allows your arguments to remain in order when dealing with the implicit "self" parameter

#

And yes, that means that this is also a potential solution:

spawnBrick.SpawnKillbrickAbovePlayer(spawnBrick, playersPosition, character)
#

You could also pass nil. Anything really, as long as you offset the arguments by one