How to constantly check a value

Hey there,

I am currently developing a game and are having trouble with a script.

The script should check a value until it hits a certain number.

Here is the script:

while true do
		if heatValue.Value >= 10 then -- Set to 10 due to testing
			print("Generators overheating")
			Alarm.Motor.alarm:Play()
		end
			end

Nothing is being printed and the alarm sound doesn’t play.

Thanks for the assistance! :grin:

6 Likes

Is this the only loop in your script, if you have others it may be stopping the script further up.

Also i dont advice any loops without waits

1 Like

If it’s a Value, there is a handy event called Changed, just do

heatValue.Changed:Connect(function()
     if heatValue.Value >= 10 then
          print("Generators overheating")
          Alarm.Motor.alarm:Play()
     end
end)
4 Likes

no make it

heatValue.Changed

Changed works with Instances!

6 Likes

Whoops! Slight mistake, thanks for the catch :wink:

2 Likes

Well, first of all. Add a wait:

while wait(.01) do
		if heatValue.Value >= 10 then -- Set to 10 due to testing
			print("Generators overheating")
			Alarm.Motor.alarm:Play()
		end
			end

Also, I don’t recommend using loops as it affects performance. Use a Changed function for this.

3 Likes

acuaro and D0RYU basically spelled it out, but if i can throw my two cents in, while true is generally a bad idea under any circumstances, because typically it’s more wasteful than other alternatives.

Also, without any waits or sections that yield, the code just runs as fast as it can, bringing any hardware it’s running on to its knees.

just my 2 sentences of unsolicited advice :slight_smile: good luck on your game!

4 Likes

What’s the full code look like? You’re not increasing the heat in this.

1 Like

Thanks to you and everyone else for the help! :grin:

There is another script that is increasing the value

1 Like