I need help with this piece of code

I’m attempting a game, went through something like this:

Localscript

local Module1 = require(replicated.Module1)

Module1:Do("yeah")

Module1

local module = {}

local Module2 = require(script.Module2)

function module:Do(word)
    Module2[word].Function()
end

return module

Module2

local module = {}
local RunService = game:GetService("RunService")

local function clientFunction()
    print("foo")
end

local function serverFunction()
    print("bar")
end

module.yeah = {
    --Name = "",
    Function = function()
        if RunService:IsClient() then
            return clientFunction
        elseif RunService:IsServer() then
            return serverFunction
        end
    end
}

return module

I’m trying to make so that the “Function” property changes depending on who is accessing the module. However, “clientFunction” never runs… What am i doing of wrong??

1 Like

You didn’t call the functions, you just returned them

module.yeah = {
    --Name = "",
    Function = function()
        if RunService:IsClient() then
            return clientFunction() -- call it
        elseif RunService:IsServer() then
            return serverFunction() -- same thing
        end
    end
}

Module2 returns the function so that Module1 can call it!

Module1

And i’m using Module2 to store multiple ‘yeahs’, all with a function that can switch to another depending on who accesses it:

Module2

local function clientFunction()
    print("foo")
end

local function serverFunction()
    print("bar")
end

local function customFunction1()
    print("yay")
end

local function customFunction2()
    print("nay")
end

module.yeah1 = {
    Name = "yeah1",
    Function = function()
        if RunService:IsClient() then
            return customFunction2
        elseif RunService:IsServer() then
            return serverFunction
        end
    end
}
module.yeah2 = {
    Name = "yeah2",
    Function = function()
        if RunService:IsClient() then
            return clientFunction
        elseif RunService:IsServer() then
            return customFunction1
        end
    end
}

Is my syntax wrong or should i do this another way? :nerd_face:

I know that, I was referring to the to the local functions


The functions you’re returning are not getting called by anything. You’re returning the memory. In Module1, simply do:

function module:Do(word)
    Module2[word].Function()()
end

or

function module:Do(word)
    local func = Module2[word].Function()
    func()
end
1 Like

Now i get it!!! thanks for the help :smiley:

1 Like