How can i use changed event in variables?

  1. What do you want to achieve? I want to check when a variable changed

  2. What is the issue? im using changed event but its not working
    that is my code, but this code is not working

local var = false
while true do
	var = true
	wait(1)
	var= false
	wait(1)
end
var.Changed:Connect(function()
	print("yes")
end)

is there a way to check when changed?

local var = Instance.new("BoolValue")

while true do
	var.Value = true
	wait(1)
	var.Value = false
	wait(1)
end
var.Changed:Connect(function()
	print("yes")
end)

Changing it to a bool value and using the .Changed event should work.

Apologies, my previous response was under the assumption is was already a bool value.

its not working, i changed my code little bit still not working :confused:

local var = Instance.new("BoolValue")

while true do
	var.Value = true
	wait(1)
	var.Value= false
	wait(1)
end


var:GetPropertyChangedSignal("Value"):Connect(function()
	print("yes")
end)

Apologies, I gave the wrong sequence of code.

We would need to implement the :GetPropertyChangedSignal event before the loop, to make sure it registers.

local var = Instance.new("BoolValue")

var:GetPropertyChangedSignal("Value"):Connect(function()
	print("yes")
end)

while true do
	var.Value = true
	wait(1)
	var.Value= false
	wait(1)
end

The issue with your code is that you are trying to use the Changed event on a variable that is not a Roblox instance. The Changed event is a feature of Roblox instances such as Part , Script , LocalScript , and many others, but not regular variables.

If you want to check when a regular variable has changed, you will need to create your own function to do so. One way to achieve this is to use a combination of a getter and setter function. Here’s an example:

local myVar = false

function getMyVar()
    return myVar
end

function setMyVar(value)
    if value ~= myVar then
        myVar = value
        print("myVar has changed!")
    end
end

setMyVar(true)
setMyVar(false)
1 Like

Um sorry i didnt saw your post. Its working thank you so much

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