How would I do something when a certain condition is met?

So, I’d like a door to open as soon as a value reaches 4, But I’m not sure how I’d make the condition for 4

I’ve tried it like this:

while true do
	if script.Parent.Value == 2 then
print(script.Parent.Value)
wait(1)
	end
end

But there’s no output.

Just saying, this was my test script, not the door script.

2 Likes

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?

3 Likes

Can you provide more info on what kind of value this is and how it changes?

An elseif statement

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

Yep, It was the value.
Anyway, thanks for the help!

1 Like

Huh, I didn’t know how to divide with roblox’s scripting system. Thanks for the tip!

1 Like

% is modulus, / is to divide normally.

1 Like

Since its a number value he would only have to do

script.Parent.Changed:Connect(onValueChanged)
1 Like

Indeed, that is a viable option however that’ll fire when any of the NumberValue’s properties are changed.

That is the case for most instances but, on the roblox API, it says that for value objects only the value property fires .Changed

1 Like

^

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.

1 Like

Would there be any exact benefit from using Changed over GetPropertyChangedSignal in this scenario like a performance difference?

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.

I did some testing on how fast they responded:

  • 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:
    image
    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

Yeah I had a feeling. The main neck here is that GetPropertyChangedSignal is a function call while Changed is direct indexation of an instance member.

2 Likes