Why doesn't this work?

I’m making a stage selector and when I click the Forward button, it behaves normally and goes back to 1:

local back = script.Parent:FindFirstChild("Back")
local forw = script.Parent:FindFirstChild("Forward")

forw.MouseButton1Click:Connect(function()
	if currentStage.Value == player:WaitForChild("leaderstats").Stage.Value then
		currentStage.Value = 1
	else
		currentStage.Value += 1
	end
end)

However, currentStage.Value does not equal Stage.Value when Stage.Value == 1. Instead, it just goes below 1:

back.MouseButton1Click:Connect(function()
	if currentStage.Value == 1 then
		currentStage.Value = player:WaitForChild("leaderstats").Stage.Value -- this doesn't work
	else
		currentStage.Value -= 1
	end
end)

Could anyone help? My explanation might be confusing, feel free to ask for more clarification.

Hmm I’m a bit confused as to what your overall goal is and what the problem is. But is the problem that when you go below 0 you want it to loop back to stage 160, but instead it goes to -1, -2, etc.?

Yeah, that’s my exact issue. When the GUI says 1 and you click the back arrow, it should go back to the player’s stage number instead of going below 1.

Give this a try and let me know what happens, just as a check to make sure it’s not having floating point “errors”:

back.MouseButton1Click:Connect(function()
	if currentStage.Value >= 0.8 and <= 1.2 then
        print("Ran")
		currentStage.Value = player:WaitForChild("leaderstats").Stage.Value -- this doesn't work
	else
		currentStage.Value -= 1
	end
end)

EDIT: Added a print() to check if it runs that part

Well if it does not equal 1 what would it equal? Try adding a print after “else” to see what it prints.

Edit: Sorry I thought it said ~=1 lol.

@MJTFreeTime Sadly didn’t work, it made the whole GUI unresponsive
@rc8s It’s supposed to equal the player’s Stage (the latest stage they have reached).

1 Like

On a low level there are floating point decimal innacurracies that happen. For example, try running this code and see what happens:

local num1 = 1.2
local num2 = 1.0

local result = num1 - num2

print(string.format("%.20f", result))

You’d expect 0.2 to be the result right? Wrong. Because of internal innacuracies due to how binary code works in the computer itself. In fact, most of what’s said about it goes over my head as well, but I get the gist of it at least.

Take a look at this article on it: http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm#:~:text=It’s%20a%20problem%20caused%20by,resulting%20in%20small%20roundoff%20errors.

Edit: Whoops just read that :joy:

“currentStage.Value += 1” what are you trying to do here?

Could you try running your original code, and place a print() inside of it, like so?:

back.MouseButton1Click:Connect(function()
	if currentStage.Value == 1 then
		currentStage.Value = player:WaitForChild("leaderstats").Stage.Value -- this doesn't work
	else
		currentStage.Value -= 1
	end
end)

It could be that it’s never identifying currentStage.Value as equal to 1 in the first place (just checking).

1 Like

I’ll read the code for you

back.MouseButton1Click:Connect(function() 
-- player clicks button
	if currentStage.Value == 1 then 
-- if their currentStage (which is the stage displayed on the GUI) is 1, then
		currentStage.Value = player:WaitForChild("leaderstats").Stage.Value
-- the GUI should equal their stage on the leaderboard
	else
		currentStage.Value -= 1
-- if not 1, then -1 from currentStage.Value
	end
end)

meaning, if currentStage.Value does not equal 1, then add 1

I’ve actually added a print() and it still didn’t show up on the console

It’s a compound operator (somewhat) recently introduced in Luau.

local num = 0

num += 1
-- Same thing as:
num = num + 1

num -= 1
-- Same thing as:
num = num - 1

-- etc.

~= means not equal to from what I know.

Yes, but I’m doing -= not ~=

Oh @lluckvy, sorry I just realized I messed up the code earlier lol!

Here it is fixed to account for floating point innaccuracies:

back.MouseButton1Click:Connect(function()
	if currentStage.Value >= 0.8 and currentStage.Value <= 1.2 then
        print("Ran")
		currentStage.Value = player:WaitForChild("leaderstats").Stage.Value -- this doesn't work
	else
		currentStage.Value -= 1
	end
end)
1 Like

But aren’t you trying to check if currentstage.Value does not equal 1?

If you look closely, it’s the substaction symbol he used, not the ~ sign. And that post I did earlier shows examples of compound operators. There’s += for addition, -= for subtraction, *= for multiplication, etc. I really like them as they’re handy and awesome in my opinion haha.

1 Like

I know he used -=, but he said this:

Oh yeah, because his conditional statement before the += checked if it’s equal to 1. Then he used else along with currentStage.Value += 1, meaning that if the conditional statement checking if it’s equal to 1 returns false, it’ll run the else section which happens to include that line of code. You probably got confused because he seemed to be accidently implying that += checks if it’s not equal to 1, when it really just adds 1. Hope this helps clarify that.

1 Like

Got this error -


and the back button when clicked still didn’t loop. However, it stopped going below 1