Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is it possible for variables to have something similar to .Changed event?

Usually, almost every object has the .Changed event, so does variables in scripts have those? It would be very useful and easy to use

For example:

local var = true
-- if the value of the 'var' changes then a command will be triggered

2 answers

Nope this is not possible. However you could do something like this


local last = varYouWantedToTrack;
while true do
  wait()
  if varYouWantedToTrack ~= last then
    -- It changed. How nice.
  end
  last = varYouWantedToTrack;
end
end);

1 comment

0

What about my idea? In the answers

How about this?

local previous_var = true
local var_that_is_going_to_be_changed = true
local overlap_prevention = false

while wait() do
	if previous_var ~= var_that_is_going_to_be_changed then
		previous_var = var_that_is_going_to_be_changed -- set the new value
		-- do stuff
	end
end

3 comments

0

That should work fine. Just seems unnecessarily complex ( not hard ) but code heavy if you do this multiple times.

0

I don't think you event need the overlap_prevention, because as soon as you set the previous_var's new value, the if statement becomes false

0

What do you mean it's unnecessarily complex casper? It is the same as yours only a little more compact.

This discussion is preserved as it was. New answers, comments, and votes are closed.