In the code used you used 2 instead of 4? Anyhow, instead of using a while loop - which can be a less efficient method - you should use a GetPropertyChangedSignal event.
Example:
local function onValueChange()
if script.Parent.Value == 4 then
print(script.Parent.Value)
end
end
script.Parent:GetPropertyChangedSignal("Value"):Connect(onValueChange)
I’m guessing here that script.Parent is the IntValue/NumberValue right?
while true do
if script.Parent.Value == 2 then
print(script.Parent.Value)
elseif script.Parent.Value == 4 then
print(script.Parent.Value)
end
wait(1)
end
Alternatively, if you want something to happen every even number you can use modulus (check the remainder of the number when it is divided by 2, so if number % 2 == 0 then it is an even number)
while true do
if script.Parent.Value % 2 == 0 then
print(script.Parent.Value)
end
wait(1)
end
ValueObjects have a special version of Changed that overrides Instance.Changed. It fires when the value is changed and passes the new value as a parameter. GetPropertyChangedSignal is not required in this scenario. Even if it fires for other properties, there’s really only two to be concerned of: Name and Parent. Doesn’t take much of a toll for performance.
Really, no. The Changed signal for ValueObjects doesn’t fire with a property meaning that it only fires when the value changes. As it only fires for a change to one property, there would be no difference. GetPropertyChangedSignal would probably be less efficient by half an atom of a hair since you call a function to retrieve the signal whereas Changed is a direct connection to the signal.
I only have a twinge of doubt since I don’t have access to a computer to test right now.
Script1. I printed the line before I changed it every 2 seconds in a while loop.
Script2. I connected on :GetPropertyChangedSignal(“Value”) and printed tick()
Script3. I connected on .Changed and printed tick()
The results:
And as you can see GetPropertyChanged signal is fractionally slower so if you really did care about those micro-optimizations then you should go for .Changed