Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make it so that you can't skip a checkpoint??

To expand more on the title, I mean how to make it so that you don't go from e.g Stage 1 to Stage 3 without touching the Stage 2 checkpoint. I am asking this because say for instance you have the flying carpet gamepass, you can fly from Stage 1 straight to the last checkpoint without having to stop at each checkpoint and I don't want that to happen.

Here is the script:

game.Players.CharacterAutoLoads = false


local dss = game:GetService("DataStoreService")
local obbyDS = dss:GetDataStore("ObbyData")


local checkpoints = workspace:WaitForChild("Checkpoints")



game.Players.PlayerAdded:Connect(function(plr)

	local levelData
	pcall(function()
		levelData = obbyDS:GetAsync("Level-" .. plr.UserId)
	
	if levelData then
		
		print(levelData)
		
		else
			
		print(plr.Name.."has no data.")
		end
	end)
	

	
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = plr

	local level = Instance.new("StringValue")
	level.Name = "Level"
	level.Value = levelData or "0"
	level.Parent = ls


	plr.CharacterAdded:Connect(function(char)

		local hrp = char:WaitForChild("HumanoidRootPart")

		hrp:GetPropertyChangedSignal("CFrame"):Wait()

		hrp.CFrame = checkpoints[level.Value].CFrame


		char.Humanoid.Died:Connect(function()

			plr:LoadCharacter()
		end)


		char.Humanoid.Touched:Connect(function(part)

			if part.Parent == checkpoints then


				if part.Name ~= "End" and level.Value ~= "End" then

					if part.Name > level.Value then

						level.Value = part.Name
					end

				elseif part.Name == "End" then
					level.Value = part.Name
				end
			end
		end)
	end)


	plr:LoadCharacter()
end)


local function saveData(plr)
	local success,errormsg = pcall(function()
		obbyDS:SetAsync("Level-" .. plr.UserId, plr.leaderstats.Level.Value)
		end)
	
	if not success then
		return errormsg
	end
end


game.Players.PlayerRemoving:Connect(function(plr)

local errormsg = saveData(plr)

if errormsg then
	warn(errormsg)
	end
end)

game:BindToClose(function()


	for i, plr in pairs(game.Players:GetPlayers()) do

		local errormsg = saveData(plr)
		if errormsg then
			warn(errormsg)
		end
	end
	wait(2)
end)

1 comment

0

Cant you scale the checkpoint so that you have to go through it to actually do the next stage?

1 answer

✓ Accepted answer

If on line 62 you are checking if current level is higher than level of the checkpoint, you need to check if checkpoint is your current level + 1. You current have

if part.Name > level.Value then
	...

And to fix it you would change it to

if (tonumber(level.Value) + 1 == tonumber(part.Name)) then
	...

tonumber will change string to number if it's possible, if not it will return nil. So here if next level is 5 and current level is 4 then 4 + 1 = 5 which means you will be able to change it, otherwise, you won't.

1 comment

0

Thanks man

This discussion is preserved as it was. New answers, comments, and votes are closed.