Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Help using moduleScripts and Parameters?

Asked by 4 years ago

So, I would like to have a moduleScript that has a function that does something to a player, and I need to pass in something about the player to the function. So far, I have this:

--Script: 
local CollisionSphereModule = require(script.Parent:WaitForChild("CollisionSphere"))
CollisionSphereModule:addCollisionSphere(game.Workspace:WaitForChild("JoRyJuKy"))

--ModuleScript:
local module = {}

function module.addCollisionSphere(Character)
    local player = game.Players:GetPlayerFromCharacter(Character)
    print(player.Name)

end

return module

And so far, it returns this error:

--Unable to cast value to Object
--(From Line 4)

What I think is that the parameter I'm passing is being treated as a value instead of an object, like the error says. Is there any way I can remedy this?

Thanks, JoRyJuky

1 answer

Log in to vote
0
Answered by
Sulfone 141
4 years ago

You defined the function with a dot, but called it a colon. For reference function m:f(x) is equivalent to function m.f(self, x) and m:f(10) is shorthand for m.f(m, 10). So in the call, the module table itself is passed as the first argument (as Character). The module table being passed to GetPlayerFromCharacter goes on to cause the error.

To fix this, line 3 can be changed to use a dot instead of colon :

CollisionSphereModule.addCollisionSphere(game.Workspace:WaitForChild("JoRyJuKy"))

Alternatively if you'll prefer to use : instead, the function definition's dot could be changed to a colon instead:

function module:addCollisionSphere(Character)

(Of course, make only one of these changes!).

0
Thanks! Works a treat! JoRyJuKy 32 — 4y
Ad

Answer this question