Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help using moduleScripts and Parameters?

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

✓ Accepted answer

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!).

1 comment

0

Thanks! Works a treat!

This discussion is preserved as it was. New answers, comments, and votes are closed.