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
@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).
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.
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).
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
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)
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.
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.