Detecting when value inside Modulescript changes

Hello, I’m trying to write a script that updates a GUI text value when a value inside a module script has changed (I.e., changing GUI text from true to false if the value inside the module script has changed). I have tried using it.Changed and :GetPropertyChangedSignal(), but as far as I understand, they won’t work. Is there a proper way to check if the value has changed within a module script, should I just use a loop script to check?

--module
local module = {
	["Water"] = false,
	["Food"] = false
}

return module

-- local script
module.Water.Changed:Connect(function()
	GUI.Text = (module.Water) 
end)

Thanks for the help in advance!

To use a Changed event, the easier way to go about it is to just use ValueObjects (e.g: a BoolValue) and then wiring those Changed events to a function.

You can do the same thing with attributes as well, creating a bunch of attributes to the ModuleScript perhaps, and then using the GetAttributeChangedSignal to retrieve an event object for when ever the attribute changes.

Keeping your same approach, here is one option:

local module = {
["Water"] = Instance.new("BoolValue"); --//Initially has the value false
["Food"] = Instance.new("BoolValue");
}

--//client code

module.Water:GetPropertyChangedSignal("Value"):Connect(function()
--//stuff
end)
2 Likes

I think you are overcomplicating it. When you change the value, just do something that you want to be done when that value is changed. To avoid code repetetion, you can create a function that changes the value and also does something else:

local function changeWater(value)
  module.Water = value
  GUI.Text = value
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.