Can't call a function in a modulescript from a script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to get my script working like normal.

  1. What is the issue? Include screenshots / videos if possible!

I keep getting an error when trying to call my function from my module script:
Workspace.Baldi.AI:6: attempt to index nil with ‘statecalc’

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes, I tried making sure it was stored in the table, which It was (Sorta):
module.statecalc = function()

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--AI Script
local statedict = require(script.State.StateDict)
local wander = require(script.Parent.Modules.Wander)

while true do
	wait(3)
	print(statedict.statecalc())
	local calc = statedict.statecalc()
	local mod1 = "script.Parent.Modules"
	local mod2 = "main()"
	local mod3 = mod1..calc..mod2
	local mod4 = mod3.split(".")
	print(mod4)
end
--Module Script
local module = {}
local states = {
	["0"] = "Idle",
	["1"] = "Wander"
}

module.statecalc = function()
	local val = script.Parent.Value
	local valstr = tostring(val)
	result = states[valstr]
end

return result

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

What is script.State? Never heard of it?

My bad, it is a value that is a parent of the module script:


Right now the value is equal to 0

It could be script.Parent.moduleName.valueName

What does?
The Value has to be parent of module script?
Or script has to be parent of module script?
(Or something else)

Did the answer above solve the problem?

No, it still says it tries to index nil with statecalc :frowning:

You are not returning module which has the function, recheck your code

1 Like

AI Script

local statedict = require(script.Parent.StateDict)
local wander = require(script.Parent.Modules.Wander)

while true do
	wait(3)
	print(statedict.statecalc())
	local calc = statedict.statecalc()
	local mod1 = "script.Parent.Modules"
	local mod2 = "main()"
	local mod3 = mod1..calc..mod2
	local mod4 = mod3.split(".")
	print(mod4)
end

Module Script

local module = {}
local states = {
	["0"] = "Idle",
	["1"] = "Wander"
}

module.statecalc = function()
	local val = script.State.Value
	local valstr = tostring(val)
	result = states[valstr]
end

return result

I fixed the location changes but it still doesn’t work

Then what are you returning here?

The variable: Result

--local val = script.State.Value
--local valstr = tostring(val)
result = states[valstr]

To refer to the value instance named “State” you’d need to use “script.Parent” as it’s the scripts parent.

“script.Parent.Value” would hence refer to the value stored by that value instance.

1 Like

image
I moved it, and fixed the code:

local val = script.State.Value
local statedict = require(script.Parent.StateDict)

Can you show us the whole ModuleScript code or is that all of it?

The one in the main post is all of it.
Btw to clarify this is the error if you haven’t noticed:
image

Ah ok then, here is your issue and also solution:

local module = {}
local states = {
	["0"] = "Idle",
	["1"] = "Wander"
}

module.statecalc = function()
	local val = script.State.Value
	local valstr = tostring(val)
	result = states[valstr]

       return result
end

return module

This should work, sorry for the bad formatting I am on mobile, the reason this wasn’t working is because you weren’t telling the other script what was inside this module using the return module line

2 Likes

That’s not how it works. When the ModuleScript initially runs, result does not yet exist and as a result, will be nil.

Your code should look like:

local module = {}
local states = {
	["0"] = "Idle",
	["1"] = "Wander"
}

module.statecalc = function()
	local val = script.State.Value
	local valstr = tostring(val)
	return states[valstr] --return the value from here
end

return module --return the table to all of the scripts
2 Likes

Yoo IT worked! :smiley: Thanks dude!

1 Like

Yours was also the same, thanks to you aswell!

1 Like

This was simply what I was telling

1 Like