I’m creating a Settings menu for my game and all of the settings are stored within a ModuleScript. The settings can change and work, but scripts outside of the modulescript that are supposed to recognize scripts within the ModuleScript have changed do not recognize that there is a change. What could be the cause of this? I’ve searched through the DevForums, but have yet to find a good answer.
Code that is supposed to recognize a value was changed in the ModuleScript:
To clarify, SettingsModule is the result of the require() function, and the settings are being stored as values inside of a table in that ModuleScript rather than as instances parented to it?
If so, one way to fix this is to have a main table and a “proxy” table in the modulescript. The proxy table will hold all the contents, while the main table will start off empty so that the metamethods __newindex and __index can fire.
Modulescript:
local main = {}
local proxy = {}
-- set all your values here:
proxy.foo = "bar"
-- connect the Changed event:
-- the event will not fire for any variables declared above this line
local event = Instance.new("BindableEvent")
proxy.Changed = event.Event
-- set metatables:
return setmetatable(main, {
__index = proxy,
__newindex = function(t, i, v)
if t[i] ~= v then
proxy[i] = v
event:Fire(i, v)
end
end
})
Script:
local SettingsModule = require()--location of the module
SettingsModule.Changed:Connect(function(PropertyName, NewValue)
print(tostring(PropertyName) .. " has changed to " .. tostring(NewValue))
end)
SettingsModule.ExampleValue = true
-- ExampleValue has changed to true
SettingsModule.ExampleValue = false
-- ExampleValue has changed to false
Not sure if this is the cleanest or most efficient solution, but it has worked for me in the past.
The value’s name is “FPS.” I implemented this code and it’s still not working. I changed SettingsModule.Value to SettingsModule.FPS and changed (“Value”) to FPS and left (“Value”) as is but neither recognized a change.
I tried this and it still doesn’t register as changed. Also, yes the settings are being stored as values inside of a table and SettingsModule is a require() leading to the module.
main {} in your script looks like the script below in my script.
I don’t see why that shouldn’t work, as long as you declare and initialize it before you connect the Changed event
Of course, if you want the variable to be named anything other than “proxy” you will have to replace the other occurences of the word “proxy” in the script I wrote.