Obby Stage Selector that goes by 10 stages?

So I have an obby stage selector that works perfectly fine in regards to it can cycle through one stage at a time. But I tried to make another button added onto it to make it go by 10 stages but, it goes into the negatives each and every time. I do have a text box input for the middle one as well but I ended up disabling the ability to type in it because I found out you could glitch it by entering a number and hitting one of the arrows. It ends up taking you to the stage anyway even if you don’t have access to it. I’ll include my three scripts for my input, left button, and right button.
Input:

local Rep = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

script.Parent.Text = tostring(game:GetService("Players").LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Stage").Value) -- updates stage number on death

script.Parent.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		Rep:WaitForChild("StageSelector"):FireServer(script.Parent.Text)
	end
end)

Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Stage").Changed:Connect(function(val)
	script.Parent.Text = tostring(game:GetService("Players").LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Stage").Value) -- updates stage number on advancing
end)

Left button:

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character
local character = player.Character
local Left = script.Parent
local text = script.Parent.Parent.Input
local checkpoints = game.Workspace.Checkpoints

Left.MouseButton1Click:Connect(function()
	local stage = game.Players.LocalPlayer.leaderstats.Stage

	if tonumber(text.Text) == 0 then
		text.Text = stage.Value + 1
	end
	text.Text = tostring(tonumber(text.Text) - 1)
	character:PivotTo(checkpoints[text.Text].CFrame + Vector3.new(0, 5, 0))
end)

Right button:

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character
local character = player.Character
local Right = script.Parent
local text = script.Parent.Parent.Input
local checkpoints = game.Workspace.Checkpoints

Right.MouseButton1Click:Connect(function()

	local stage = game.Players.LocalPlayer.leaderstats.Stage
	if tonumber(text.Text) == stage.Value then

		text.Text = -1

	end

	text.Text = tostring(tonumber(text.Text) + 1)

	character:PivotTo(checkpoints[text.Text].CFrame + Vector3.new(0, 5, 0))

end)

(I tried to do a stage selector by 10 stage by just switching the 1 to 10, which had worked but, like I said, it ended up going into the negatives.) Any help is appreciated!

it ended up going into the negatives

One way to fix that is by using math.clamp.
math.clamp is a function that returns a value that is clamped within a specified range.
Here’s an example:

math.clamp(value, minValue, maxValue)

minValue would be the lowest stage they can be at (for example 0), and maxValue would be the highest stage they can be at.

1 Like

I’m still relatively new with scripting. How would I set that up? Would It be something like this?

local minValue = 0
local maxValue = 156

Then enter in what you mentioned anywhere in the script?

math.clamp(value, minValue, maxValue)

That’s fine, here’s what you can do:

StageValue = math.clamp(Value, minValue, maxValue)
1 Like

Thank you so much for the help! I’ll go on studio to test it out later :grinning:

1 Like

You are welcome, glad I could help!

1 Like

So I made this script for the left 10 stage button but it doesn’t seem to work properly:

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character
local character = player.Character
local Left = script.Parent
local text = script.Parent.Parent.Input
local checkpoints = game.Workspace.Checkpoints

Left.MouseButton1Click:Connect(function()
	local stage = game.Players.LocalPlayer.leaderstats.Stage
	local minValue = 0
	local maxValue = 156
	local Value = stage

	if tonumber(text.Text) == 0 then
		text.Text = stage.Value + 10
		local StageValue = math.clamp(Value, minValue, maxValue)
	end
	text.Text = tostring(tonumber(text.Text) - 10)
	character:PivotTo(checkpoints[text.Text].CFrame + Vector3.new(0, 5, 0))
end)

Like when it is at 0 it has no problems but, for example, if I go to stage 1 and press the left 10 one it puts -9. I’m not sure what the problem is unless I made the script wrong.

Your script is very messy, I think that you have forgot to add .Value to the Value variable (you are trying to clamp a ValueBase instance instead of it’s value)

The local Value = stage one? Is it supposed to be local Value = stage.Value or local value.Value = stage? I’m still learning as I go in regards to scripting so I know that there are probably a variety of ways to make this in a more efficient/neater way.

just instead the line:

local StageValue = math.clamp(Value, minValue, maxValue)

do:

local StageValue = math.clamp(stage.Value, minValue, maxValue)

You have referenced the “stage” variable again (unnecesarily) as “Value”, Value is a parameter of all ValueBased instances (I assume the stage variable holds some instance of this kind)

1 Like

Ah, okay. I see what you mean now. Thanks for telling me that :smiley: Unfortunately I had already tried that too and it still is pulling the numbers into the negatives for some reason.

The line subtracts 10 from the value of text.Text , which is then converted to a string and set back to text.Text . If the original value of text.Text is less than 10, this will result in a negative number.
To fix this, you can add a check to ensure that the new value of text.Text is not less than zero. Here’s an updated version of your script with this check:

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character
local character = player.Character
local Left = script.Parent
local text = script.Parent.Parent.Input
local checkpoints = game.Workspace.Checkpoints

Left.MouseButton1Click:Connect(function()
	local stage = game.Players.LocalPlayer.leaderstats.Stage
	local minValue = 0
	local maxValue = 156
	local Value = stage

	if tonumber(text.Text) == 0 then
		text.Text = stage.Value + 10
		local StageValue = math.clamp(Value, minValue, maxValue)
	end
	local newValue = tonumber(text.Text) - 10
	if newValue >= 0 then
		text.Text = tostring(newValue)
		character:PivotTo(checkpoints[text.Text].CFrame + Vector3.new(0, 5, 0))
	end
end)

In this version of the script, the new value of text.Text is only set if it is greater than or equal to zero. If it is less than zero, the value is not changed and the character does not pivot to a checkpoint.

1 Like

Ah Thank you so much! The way you explained it led me to understand it more than I ever could I have understood it on my own. And the way you set it up in the script too makes a lot of sense and I was able to set up the script for the other button as well. So thank you for all the help!

1 Like

You are welcome.
Good luck with your project!

1 Like

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