You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to get my script working like normal.
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’
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.
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
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
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