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

I keep getting "attempt to call a nil value" error?

Asked by 5 years ago

Does anyone know whats wrong?

Localscript:

local PlacementModule = require(script.PlacementModule)
game:GetService("RunService").RenderStepped:connect(PlacementModule.OnRenderStepped)

PlacementModule:

local module = {}
    function OnRenderStepped(deltatime)
            print("RenderStepped")

        end
return module

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The problem is very simple. You don't return the function itself. You returned the table instead.

local module = {}

local function OnRenderStepped(deltatime)
    print("RenderStepped")
end
return OnRenderStepped

-- from localscript 
local PlacementModule = require(script.PlacementModule)
game:GetService("RunService").RenderStepped:Connect(PlacementModule)

Though of course you didn't want that. You wanted to return the table with its functions and such.

local module = {}

function module.OnRenderStepped(deltatime) -- function is now part of the table
    print("RenderStepped")
end

return module

-- from localscript
local PlacementModule = require(script.PlacementModule)
game:GetService("RunService").RenderStepped:Connect(PlacementModule.OnRenderStepped)

On a side note, :connect() is deprecated, use :Connect() instead.

0
Ok, thanks! erlend5005 2 — 5y
Ad

Answer this question