Previous and forward stage button for obby

I’ve been trying to make a proper working system for a forwards and backwards gui but so far i have been unsuccessful.
backwards remote script:
Runcontext: Server

game.ReplicatedStorage.back.OnServerEvent:Connect(function(plr)
	
	if plr.back.Value == 0 then
		plr.back.Value = 1
	else
		plr.back.Value = plr.back.Value+1
	end
	
	
	
	
end)

Forwards remote script:

game.ReplicatedStorage.forward.OnServerEvent:Connect(function(plr)
	print("forward passed test")
	plr.back.Value = plr.back.Value-1
end)

Backwards script so far:

local Stages = workspace:WaitForChild("Stages")

script.Parent.MouseButton1Click:Connect(function(plr)
	wait(0.5)
	local playerback = script.Parent.Parent.Parent.Parent.Parent.back.Value
game.ReplicatedStorage.back:FireServer(plr)
	local playerCheckpoint = script.Parent.Parent.Parent.Parent.Parent.leaderstats.Stage.Value
	print(playerCheckpoint)
	if playerCheckpoint > 1 then -- Ensure the player is not on the first checkpoint
		local teleportCheckpoint = game.Workspace.Stages:FindFirstChild(tostring(playerCheckpoint - 1))
		
		print(teleportCheckpoint)
		if teleportCheckpoint then
			script.Parent.Parent.Parent.Parent.Parent.Character.HumanoidRootPart.CFrame = CFrame.new(teleportCheckpoint.Position)+Vector3.new(0,2,0)
		
		end
	end
end)

Forward script so far:

local Stages = workspace:WaitForChild("Stages")
local followmeinmerrymeasure
script.Parent.MouseButton1Click:Connect(function(plr)
	wait(0.5)
	local playerback = script.Parent.Parent.Parent.Parent.Parent.forward.Value
	local playerback2 = script.Parent.Parent.Parent.Parent.Parent.back.Value
	
	local playerCheckpoint = script.Parent.Parent.Parent.Parent.Parent.leaderstats.Stage.Value
	local playerCheckpoint100 = tonumber(script.Parent.Parent.Parent.Parent.Parent.leaderstats.Stage.Value)
	
	
		
			game.ReplicatedStorage.forward:FireServer(plr)
		local targetCheckpoint = Stages:FindFirstChild(tostring(playerCheckpoint-playerback2 + 1))
	followmeinmerrymeasure = tonumber(targetCheckpoint.Name)
	if playerCheckpoint < followmeinmerrymeasure then
		return
	
			else
			if targetCheckpoint then
				script.Parent.Parent.Parent.Parent.Parent.Character.HumanoidRootPart.CFrame = CFrame.new(targetCheckpoint.Position) + Vector3.new(0, 2, 0)
			end
		
	end
end)

if any more information is needed, I am very happy to provide as I want to get this finished.

1 Like

the first problem i see is the amount of Parents

local playerback = script.Parent.Parent.Parent.Parent.Parent.forward.Value
local playerback2 = script.Parent.Parent.Parent.Parent.Parent.back.Value
	
local playerCheckpoint = script.Parent.Parent.Parent.Parent.Parent.leaderstats.Stage.Value
local playerCheckpoint100 = tonumber(script.Parent.Parent.Parent.Parent.Parent.leaderstats.Stage.Value)

...

local playerCheckpoint = script.Parent.Parent.Parent.Parent.Parent.leaderstats.Stage.Value
local playerback = script.Parent.Parent.Parent.Parent.Parent.back.Value

get the player like this instead

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

-- Now you can get the player's stuff properly
local Back = Player:WaitForChild("Back")

also this can be shortened

if plr.back.Value == 0 then
  plr.back.Value = 1
else
  plr.back.Value = plr.back.Value+1
end
-- plr -> Player
-- "if value is 0 then set value to 1 else if value isn't 0 then add 1"
-- just add 1 without checking
Player.Back.Value += 1

I took your suggestion and played around with it a bit and realized that I never specified the name while I was writing the datastore portion of the CurrentStage stat. I had also given you the wrong script as well. I had provided a past local script on accident. Then after realizing in the handler that I never specified the correct path for the player to teleport to, I fixed it up and it worked. Thank you for taking the time out of your day to provide code.

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