How to detect if a value has changed?

I am very new to scripting, and I am messing around in studio trying to teach myself the ropes. What I am currently trying to do is add a number to a table every time it changes.
More specifically, I have a value in the workspace, and I want to add its value to a table each time it changes, but I have no idea how to go about doing this. Any help is appreciated.

22 Likes

All isntances have a Instance:GetPropertyChangedSignal(PropertyName) function which returns the event fired when that particular property is fired. For example, to detect a value change you would do:

local value = game.Workspace.NumberValue

value:GetPropertyChangedSignal("Value"):Connect(function()
    print("New value: " .. value.Value)
end)

wait(5)
value.Value = 4

After 5 seconds, you should see this in the console: image

Credit to @xZylter who corrected me

58 Likes

This is perfect, thanks! (30 chars)

4 Likes

You should use Instance:GetPropertyChangedSignal("Property Name") instead of .Changed since .Changed fires whenever any property is changed such as the Name or Parent

11 Likes
5 Likes

how would I make this so it would say if the value changed(ie had value added/removed)

If you mean whether a new ValueObject has been added or removed you can use the .ChildAdded / .ChildRemoved events.

1 Like

no like if a int value is higher or lower then the target value

You can always just do this.

-- Sorry for poor formatting.
local numValue = workspace.NumberValue

local currentNumber = numValue.Value

numValue:GetPropertyChangedSignal("Value"):Connect(function()
    if currentNumber > numValue.Value then
      print("It's more than the number value.")
else
     print("It's less than the number value.")
end
    currentNumber = numValue.Value
end)

oh right sorry about that (CHARRRRR)

You can also use while loops

local Cash= game.Workspace.Cash

while task.wait(0.0001) do
script.Parent.Text = Cash.Value
end

Or, for simplicity, you could use the .Changed event, which fires every time any property of an object is changed. So as long as you’re not changing the name or parent of said value, .Changed should work perfectly.

If you have an instanceless value, then I suggest using bindable events.

local myBindableEvent = Instance.new("BindableEvent")

-- Function to set a new value and fire the BindableEvent
local function setValue(value, newValue)
	-- Update the value with the new one
	value = newValue

	-- Fire the BindableEvent with the new value
	myBindableEvent:Fire(newValue)

	-- Return the new value
	return newValue
end

-- Function to handle the event when the value changes
local function onValueChanged(newValue)
	print("Value changed to:", newValue)
end

-- Connect the onValueChanged function to the BindableEvent's Event
myBindableEvent.Event:Connect(onValueChanged)

--// Example usage \\--
local exampleValue = 0

-- Continuously update the exampleValue using the setValue function
while true do
	-- Increment the exampleValue by 1 and set the new value
	exampleValue = setValue(exampleValue, exampleValue + 1)
	task.wait(5)
end

sorry to revive such an old topic, but: don’t do this. While loops, when used in Python or any other slightly higher-level languages, are mostly fine, but in Roblox, you should be using game:GetService(“RunService”).RenderStepped:Connect(function() print(“Hello World!”) end) alongside any conditionals you may have.

2 Likes

yeah sure you could use that too, but i think theyr the same thing.

1 Like

RenderStepped/Stepped functions inside of the RunService either run after a frame has been rendered, or prior. while task.wait() do might be “functionally” the same, however, many features are missed out on (such as delta time for timers.) It’s generally better practice to use RunService.RenderStepped (or just RunService.Stepped if on the server.)

1 Like