How to trigger events by a value?

Hey folks. Knew it wouldn’t be long until I was back here again.
Anyway, (reactor core game for context) I’m trying to make a situation where once the temperature reaches a certain number, the core will start smoking. Once it lowers past that point, the smoke disables again.
Here’s my script.

local smoke = script.Parent
local heat = game.Workspace.Values["THE HEAT"]
smoke.Enabled = false

if heat.Value >= 2700 then
	while true do
		smoke.Enabled = true
	end
end

if heat.Value <= 2700 then
	while true do
		smoke.Enabled = false
	end
end

Am I making little mistakes here or am I not even close?
Also, does script placement matter; what its parent is?

1 Like

dios mio

you are doing “while true do” with 0 delay and never break the loop

Try this:

local smoke = script.Parent
local heat = game.Workspace:WaitForChild("Values"):WaitForChild("THE HEAT")
smoke.Enabled = false

while true do
wait(1)
if heat.Value >= 2700 then
smoke.Enabled = true
else
smoke.Enabled = false
end end

(Place script INSIDE the smoke effect)

This should do the job.

heat:GetPropertyChangedSignal("Value"):Connect(function()

    -- Do checks

end)

:GetPropertyChangedSignal DevHub

Just realized I’m a first-class moron. I thought I had the smoke parented to the script the whole time—nope, it was right underneath it. I don’t think I would have caught that if you didn’t tell me to check the output. I really need my eyes checked.

Anyway, this script works great. And it’s less clunky than my prototype. I’ll compare it to mine so I can learn from it for future code, thanks!

No problem! Glad I could help. The DevForum is overpowered :wink:

1 Like