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

Can't understand this module error?

Asked by 8 years ago

I've looked at this over and over, and can't figure out what's going wrong

ServerScript

game:GetService('Players').PlayerAdded:connect(function(player)
    local api = require(script.Parent.Data)
    api:CheckGroups(player)
end)

ModuleScript

function module.CheckGroups(player)
    print(player.Name)
end

The modulescript only prints 'nil'

When I try to use player:IsInGroup() in the module script it returns an error.

Thanks in advance for the help.

1 answer

Log in to vote
0
Answered by 8 years ago

Your function is not designed as a method
When you use : as the accessor for a function, it is being accessed as a method. Methods are special in that they tell the function where they came from when they're called.

In your code, you write this as your function

function module.CheckGroups(player)
    print(player.Name)
end

Which means that when you later called api:CheckGroups(player), you actually called api.CheckGroups(api, player). As module (api) has no entry for Name, it printed nil. The solution is either to do

api.CheckGroups(player)

Or alternatively to declare the function explicitly as a method as so

function module:CheckGroups(player)
    print(player.Name)
end

Still not sure? When you call a method, it calls the function with the source as the first argument. It's syntactic sugar for OOP-style methods.

0
Thanks, I knew it was something silly like that. Verbero 0 — 8y
Ad

Answer this question