I am creating a module that handles settings for games. I am adding signals to it using the Signals module that fire whenever a setting is changed.
However, it’s not working. Not the signal, but detecting if the setting has changed.
local function heartbeat()
for name, setting in pairs(settingsModule.Settings) do
local oldSetting = oldSettings[name]
if oldSetting then
if oldSetting.Value ~= setting.Value then
settingsModule.SettingChanged:Fire(setting)
setting.Changed:Fire(setting.Value)
end
else
settingsModule.SettingAdded:Fire(setting)
end
end
oldSettings = settingsModule.Settings
end
runService.Heartbeat:Connect(heartbeat)
This is what I have for it right now. Unfortunately, the signal doesn’t fire, and if I add a print, it won’t do that either.
I thought it might be oldSettings = settingsModule.Settings, so I made it individually set each setting, but that didn’t work either.
I’m thinking the oldSettings variable could just be referencing the settingsModule.Settings table so it’s always the same, but I don’t know how I can get around that.
Any thoughts?
(Don’t be fooled by the Value “property”! The settings are not objects, they are tables!)
First of all the Signal module you use is unlisted and none of the devs ik seems to use it (not saying that you shouldn’t but these are top devs), a better pick would be stravants GoodSignal which afaik is the industry standard as it has already been benchmarked and is already proved to be superior to the competition.
Yep, here’s the stats to back it up: Lua Signal Class Comparison & Optimal `GoodSignal` Class GoodSignal is the industry standard as you say.
Your code looks good and should work as intended from what I can see.
Try disconnecting it from the RS loop, and instead run it every X seconds. Print out the oldSettings and the currentSettings → are they what you expect?
Value is always the same when I print them both out. I think this is because I’m changing oldSettings to reference settingsModule.Settings- and not actually changing oldSettings to the value of settingsModule.Settings. How can I work around this?
New code if it's useful
local function changedLoop()
while task.wait(0.1) do
print(oldSettings)
print(settingsModule.Settings)
for name, setting in pairs(settingsModule.Settings) do
local oldSetting = oldSettings[name]
if oldSetting then
if oldSetting.Value ~= setting.Value then
settingsModule.SettingChanged:Fire(setting)
setting.Changed:Fire(setting.Value)
end
else
settingsModule.SettingAdded:Fire(setting)
end
end
oldSettings = settingsModule.Settings
end
end
task.spawn(changedLoop)