How would I optimize this? or does it even make a difference?

How would I make this script run, when a value is changed instead of the script being something like:

while true do
wait()
	if value == "1" then
		--script
	end
end

and does it even affect the game if scripts are like that?

1 Like

:GetPropertyChangedSignal() is what you’re looking for.

Example:

game.Workspace.Bool:GetPropertyChangedSignal("Value"):Connect(function()
    if game.Workspace.Bool.Value == 1 then
       -- script 
    end
end)
2 Likes

Quick nitpick: BoolValues (and all of the other XyzValue objects) have a Changed property that accomplishes this in a slightly simpler manner:

workspace.Bool.Changed:Connect(function(state) 
    print(state)
end)

The “Changed” event also fires when any property such as the name is changed. So his way would be more proper. (unless you arent changing the name it should be just fine)

This is true for every instance except for the Value objects, which have their own Changed event:

So it basically saves some lines but has the same effect?

Yeah, the only difference between them is that BoolValue.Changed gets fired with the new value and BoolValue:GetPropertyChangedSignal(“Value”) doesn’t.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.