How would I make a script fire when a number value hits a certain number

Okay so lets say for example I want a script to fire when a number value hits 4 and something happens
so what I would want to achieve here is a script that checks the number values value and when it detects that the values is 4 and fires the scripts
this is something I wrote that checks for the number value

local NumberValue = workspace.NumberValue
if NumberValue.Value == 4 then
--do stuff
end

What I think the problem may be is the script checks once, sees that the number value isn’t 4 and goes to sleep, any tips on making it work?

1 Like

Try using this then you can put your statements in it.

You can use the GetPropertyChangedSignal() event (or event the Changed event) to detect when a value changes.
Here is your code:

local NumberValue = workspace:WaitForChild("NumberValue")

NumberValue:GetPropertyChangedSignal("Value"):Connect(function()
	if NumberValue.Value == 4 then
		-- your stuff
	end
end)
1 Like

if it is a NumberValue, you only need Changed

1 Like

Its still good practice to use GetPropertyChangedSignal rather then Changed.

it shouldn’t be used if you want to change a NumberValue’s Value property

local NumberValue = workspace:WaitForChild("NumberValue")

NumberValue:GetPropertyChangedSignal("Value"):Connect(function()
	if NumberValue.Value == 4 then
		-- your stuff
	end
end)
local NumberValue = workspace:WaitForChild("NumberValue")

NumberValue.Changed:Connect(function()
	if NumberValue.Value == 4 then
		-- your stuff
	end
end)

because of how NumberValues work, these two scripts work the exact same way

1 Like

Would this also be capable of firing remote events?

you can fire remote events inside a connection

local NumberValue = workspace:WaitForChild("NumberValue")

NumberValue.Changed:Connect(function()
    if NumberValue.Value == 4 then
        RemoteEvent:FireServer()
    end
end)

EDIT: I still had that script in a quote for some reason