Help with my checkpoints scripts

Good day everyone,

I am currently using a script in my Roblox guess game and i made a GUI that enables players to skip stages by purchasing a developer product. I’m encountering a challenge where I would like to allow players to return to a previous stage (e.g., from stage 3 to stage 2) by interacting with a checkpoint. Essentially, if a player touches checkpoint 3 and then touches checkpoint 2, their leaderstat for the current stage should change back to stage 2.

If necessary, I can share the script for further clarification.

Thank you for your time and assistance.

just make it so that whenever they touch it using the :Touched event it sets their leaderstat to 2

3 Likes

How do i do that? i am good at scripting…

Can you show the script please?

The checkpoints one? Do you think you can help me solve my problem?

yes. Show the others too maybe.

I think the problem you have I can solve

Here it is:

local dataStoreService = game:GetService("DataStoreService")
local stageStore = dataStoreService:GetDataStore("PlayerStageDataStore")

function plrToStage(plr)
	repeat wait() until plr.Character.HumanoidRootPart

	local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))

	plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats

	local success, errorMsg = pcall(function()
		stage.Value = stageStore:GetAsync(player.UserId)
	end)

	if success then
		print("Successfuully got "..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end

	if player.Character then
		plrToStage(player)
	end

	player.CharacterAdded:Connect(function()
		plrToStage(player)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMsg = pcall(function()
		stageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
	end)

	if success then
		print("Successfuully saved "..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end
end)

for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
	checkpoint.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			local checkpointNumber = tonumber(checkpoint.Name)

			if player.leaderstats.Stage.Value < checkpointNumber then
				player.leaderstats.Stage.Value = checkpointNumber
		end
	end
end)
end

So, you want it so the player can go to the previous stage is that it?

If yes, here’s the script:

local dataStoreService = game:GetService("DataStoreService")
local stageStore = dataStoreService:GetDataStore("PlayerStageDataStore")

function plrToStage(plr)
	repeat wait() until plr.Character.HumanoidRootPart

	local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))

	plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats

	local success, errorMsg = pcall(function()
		stage.Value = stageStore:GetAsync(player.UserId)
	end)

	if success then
		print("Successfuully got "..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end

	if player.Character then
		plrToStage(player)
	end

	player.CharacterAdded:Connect(function()
		plrToStage(player)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMsg = pcall(function()
		stageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
	end)

	if success then
		print("Successfuully saved "..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end
end)

for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
	checkpoint.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			local checkpointNumber = tonumber(checkpoint.Name)

			if  checkpointNumber == player.leaderstats.Stage.Value + 1 or checkpointNumber ==  player.leaderstats.Stage.Value - 1  then
				player.leaderstats.Stage.Value = checkpointNumber
			end
		end
	end)
end

I love you, i love you, i love you thank you so much

1 Like

No problem! I’m glad i could help you!
Mark my response as a solution so you can close this topic, since it worked

Yes i will don’t worry, i’m doing that right now

I could also recommend you making animations on your checkpoints
Im making a game, and you can take the example:

1 Like

I’ll check it out when possible, thanks for the help again. I might open another topic about my skip stage script but i’m not sure

1 Like

Ok. I can help you with that too. I will say it now that that is easy, not hard actually. easier than programming the Stages

Okay. Right now i’m about to test your game.

Script works, but not as i indented it to. It does change the stage but it does only in order, it can’t change for example from stage 6 to 2, only in order. I want it to change the stage to the stage the person is currently is, if they go to stage 5 for example and go to spawn which is stage 1, i want it to change from stage 5 to stage 1. Scripts works indeed, but not as i wanted it to. Thanks for any help!

It’s ok. I can still give you the corrected script you prompted me:

local dataStoreService = game:GetService("DataStoreService")
local stageStore = dataStoreService:GetDataStore("PlayerStageDataStore")

function plrToStage(plr)
	repeat wait() until plr.Character.HumanoidRootPart

	local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))

	plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats

	local success, errorMsg = pcall(function()
		stage.Value = stageStore:GetAsync(player.UserId)
	end)

	if success then
		print("Successfuully got "..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end

	if player.Character then
		plrToStage(player)
	end

	player.CharacterAdded:Connect(function()
		plrToStage(player)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMsg = pcall(function()
		stageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
	end)

	if success then
		print("Successfuully saved "..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end
end)

for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
	checkpoint.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			local checkpointNumber = tonumber(checkpoint.Name)

			if  checkpointNumber == player.leaderstats.Stage.Value + 1 or checkpointNumber <  player.leaderstats.Stage.Value  then
				player.leaderstats.Stage.Value = checkpointNumber
			end
		end
	end)
end
1 Like
for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
	checkpoint.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			local checkpointNumber = tonumber(checkpoint.Name)



			if  checkpointNumber == player.leaderstats.Stage.Value + 1 then
				player.leaderstats.Stage.Value = checkpointNumber
elseif checkpointNumber < player.leaderstats.Stage.Value then
player.leaderstats.Stage.Value == checkpointNumber
			end
		end
	end)
end

Note that formatting is a bit off since I did this all on the devforum but it should work still

1 Like

but you can do

if checkpointNumber == player.leaderstats.Stage.Value + 1 or checkpointNumber <  player.leaderstats.Stage.Value then

because it’s the same function trying to set the Stage to the same number (checkpointNumber)

But yes it’s still the same

1 Like

@TheCoolBaconHairIsMe

you can use mine here or @Earthraphobic2’s below if you want. They function the same expect they’re in different positions. Hope that’s the problem