Help with boolean values and surface guis

Hello!

I would like to script a system in my game that checks if two bool values are at certain values and then change a textlabel on a surfacegui in accordance to what the values are at currently, sort of like a cooling system in a nuclear reactor/reactor core game where the temperature reader goes up if the cooling system isn’t on and goes down when the cooling system is on.

Before I explain the problem, here is my script:

local value1 = game.ReplicatedStorage.boolfolder.BoolOne
local value2 = game.ReplicatedStorage.boolfolder.BoolTwo

local number = Instance.new("NumberValue")
number.Parent = script.Parent
number.Value = 400

local textlabel = script.Parent
textlabel.Text = number.Value

while true do
	
	if value1 or value2 == true then
		number.Value = number.Value + 1
	elseif value1 and value2 == true then
		number.Value = number.Value + 5
	elseif value1 and value2 == false then
		if number.Value > 400 then
			number.Value = number.Value - 1
		end
	end
	
	wait(0.75)
end

The textlabel’s text ends up changing to the number 400 when the server starts, but that’s the only thing that works. The number value never changes at all, even when I press a button that turns the boolean values off and on.

The script is supposed to make the number on the textlabel go up by five when both boolean values are true, go up by one when only one boolean value is false, and go down by one if both boolean values are false until it the number reaches 400 again.

The script I use is a regular script inside of a textlabel on a surfacegui, in case that information is needed.

I’ve tried looking everywhere, yet I can’t find a solution.

Can somebody help? Thanks!

1 Like

Couple things I have for you.

Big Tip:
It’s easier to use number.Value += 1 instead of number.Value = number.Value + 1!

1st.
You are not actually assigning any new values, you’re essentially just saying whether or not value1 and value2 are “true” or “false”, you don’t have it checking the actual value itself, all you need to do here is just say:

	if value1.Value or value2.Value == true then
		number.Value += 1
	elseif value1.Value and value2.Value == true then
		number.Value += 5
	elseif value1.Value and value2.Value == false then
		if number.Value > 400 then
			number.Value -= 1
		end
	end

2nd.
The TextLabel is only reading whatever “number.Value” is before you enter into your while loop, in this case, it’s the number 400.

You also did not actually check to see if value1 had a value of anything, you’ll need to specify both values and what you’re wanting to see from them for it to work correctly.

While loops are inefficient, especially if you use them for everything, it’s constant looping if it doesn’t update for some reason, but technically what you are wanting out of this is for it to constantly update every 0.75 seconds.

task.wait() is also a more efficient way of waiting in a script!

Try something along the lines of this:

local value1 = game.ReplicatedStorage.boolfolder.BoolOne
local value2 = game.ReplicatedStorage.boolfolder.BoolTwo

local number = Instance.new("NumberValue")
number.Parent = script.Parent
number.Value = 400

local textlabel = script.Parent

while true do
	
	task.wait(0.75)
	
	if value1.Value == true or value2.Value == true then
		number.Value += 1
	end

	if value1.Value == true and value2.Value == true then
		number.Value += 5
	end

	if value1.Value == false and value2.Value == false then
		if number.Value > 400 then
			number.Value -= 1
		end
	end

	textlabel.Text = number.Value
	
end

You should definitely check out how to use GetPropertyChangedSignal if you have the chance, could help you out a lot in the future!

Here is one example: Detect Changed Value using GetPropertyChangedSignal - #2 by BabyNinjaTime

Hope this helped!

1 Like

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