Non-hacky method to detect variable change[Fixed nevermind]

is there a non-hacky method that we can detect when a variable is changed I know we can use object value and use the Changed event but believe there is a way to do that without it
context I am making a custom character controller something like this

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		CurrentDirection=1
		Left = true
	elseif inputState == Enum.UserInputState.End then
		Left = false
		if Right then
			CurrentDirection =-1
		else
			CurrentDirection=0
		end
	end
end
 
local function onRight(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		CurrentDirection=-1
		Right = true
	elseif inputState == Enum.UserInputState.End then
		Right = false
		if Left then
			CurrentDirection =1
		else
			CurrentDirection=0
		end
	end
end

local function onUpdate()
	if Character and Character:FindFirstChild("Humanoid") then
		Character.Humanoid:Move(Vector3.new(CurrentDirection,0,0),false)
		--i want a function to play when CurrentDirection
	end
end

RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)
ContextActionService:BindAction("Left", onLeft, true, Enum.KeyCode.D)
ContextActionService:BindAction("Right", onRight, true, Enum.KeyCode.A)

i know you can save it in another variable and check if they are different the same can be done instead of .Changed event i am just looking for a simpler way to do so

2 Likes

Short answer: no.
Long answer: no.

If you want to do it to prevent exploits, then shouldn’t be doing this. Instead, have proper sanity checks on the server so that any spoofed variables on the client cannot impact the game.

If you didn’t intend to do this for anti exploit purposes, then something is wrong with your code setup.

2 Likes

No. But this method I am about to show you is in fact hacky and I do not recommend it whatsoever. It involves tables.

local real = { x = 1 }
local proxy = setmetatable({ }, {
    __index = real,
    __newindex = function(this, key, value)
        if rawget(real[key]) ~= nil then
            rawset(real, key, value)
            print("changed", rawget(real, key))
            rawset(this, key, nil)
        end
    end
})

It works. But I ask that you do not do this. Thank you.

Did somebody say .Changed for tables???

(Just set this metatable to the global environment, getfenv(), or use a table to access the variables you want to track.)

2 Likes

You can simply create a Bindable Function and when you change the variable invoke the Bindable Function.

2 Likes

Uhm context is probably useful.

If this is for any expliot security, then I completely agree with @Amiaa16. Don’t set a listener for variable changes, its not practical in security application because you will not know what changed the variable in the first place.

If its for detecting environment change to invoke a function, then theres plenty of ways to go about activating functions on change events. Many objects have changed events for specific properties, and its relatively simple to communicate code to other code with remote/bindable events and functions.