Do not know how to include

I have a script which increases the seconds’ value by one per second, increases the deaths’ value once every death. I have a stage value in the script but do not know how to add in the part where if the player touched the checkpoint they go up one stage in value.
the script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local ga = false
Players.PlayerAdded:Connect(function(Player)
	local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
	
	warn("Initializing leaderstats...")
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = Player
	local stage = Instance.new("StringValue")
	stage.Name = "Stage"
	stage.Parent = stats
	local wipeouts = Instance.new("IntValue")
	wipeouts.Name = "Deaths"
	wipeouts.Parent = stats
	local second = Instance.new("IntValue")
	second.Name = "Seconds"
	second.Parent = stats
	print("Completed.")
	
	warn("Initializing leaderstats values...")
	local success = PlayerDataStore:GetAsync("success")
	local PlayerData = PlayerDataStore:GetAsync("PlayerData")
	if success == nil or false then
		print("Player is currently nil")
		PlayerDataStore:SetAsync("success", true)
		PlayerDataStore:SetAsync("PlayerData", HttpService:JSONEncode({stage = 0, deaths = 0, sec = 0}))
	end
	local decoded = HttpService:JSONDecode(PlayerData)
	Player.leaderstats.Deaths.Value = decoded.deaths
	Player.leaderstats.Seconds.Value = decoded.sec
	Player.leaderstats.Stage.Value = decoded.stage
--	local cp = workspace.Checkpoints:FindFirstChild(decoded.stage)
--	local Hrp = workspace:WaitForChild(Player.Name).HumanoidRootPart -- Will index nil even when calling it in CharacterAdded()
--	for _,v in pairs(workspace:FindFirstChild(Player.Name):GetChildren()) do
--		print(v)
--	end
	print("Completed.")
	warn("Continuing with normal RunTime")
	Player.CharacterAdded:connect(function(Character)
		local d = true
		Character:WaitForChild("Humanoid").Died:Connect(function()
			if d then
				Player.leaderstats.Deaths.Value += 1
			end
		end)
	end)
	ReplicatedStorage.ApplyStage.Event:Connect(function(stageid)
		stage.Value = stageid
	end)
	coroutine.resume(coroutine.create(function()
		while wait(1) do
			Player.leaderstats.Seconds.Value += 1
			if ga then
				break
			end
		end
	end))
end)

Players.PlayerRemoving:Connect(function(Player)
	ga = true
	local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
	warn(string.format("%s IN QUEUE...", Player.Name:upper()))
	local death = Player.leaderstats.Deaths.Value
	local secs = Player.leaderstats.Seconds.Value
	local stage = Player.leaderstats.Stage.Value
	local data = {stage = stage, deaths = death,sec = secs}
	local encoded = HttpService:JSONEncode(data)
	PlayerDataStore:SetAsync("PlayerData", encoded)
	print("Done")
end)

the script i want to add in:

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

local checkpoints = workspace.Checkpoints


game.Players.PlayerAdded:Connect(function(plr)
	
	local obbyData = obbyDS:GetAsync(plr.UserId .. "-obbyStageProgress")
	
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = plr
	
	local stage = Instance.new("StringValue")
	stage.Name = "Stage"
	stage.Value = obbyData or 1
	stage.Parent = ls
	
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	char:WaitForChild("HumanoidRootPart").CFrame = checkpoints[stage.Value].CFrame
	
	char.Humanoid.Touched:Connect(function(touch)
				
		if touch.Parent == checkpoints then
			if (tonumber(touch.Name) and tonumber(touch.Name) > tonumber(stage.Value)) or touch.Name == "End" then
				stage.Value = touch.Name
					
				pcall(function()
			
					obbyDS:SetAsync(plr.UserId .. "-obbyStageProgress", plr.leaderstats.Stage.Value)
				end)
			end
		end
	end)
	
	plr.CharacterAdded:Connect(function(char)
		
		local hrp = char:WaitForChild("HumanoidRootPart")		
		local humanoid = char:WaitForChild("Humanoid")
			
		hrp:GetPropertyChangedSignal("CFrame"):Wait()

		hrp.CFrame = checkpoints[stage.Value].CFrame
		
		humanoid.Touched:Connect(function(touch)
				
			if touch.Parent == checkpoints then
				if (tonumber(touch.Name) and tonumber(touch.Name) > tonumber(stage.Value)) or touch.Name == "End" then
					stage.Value = touch.Name
					
					pcall(function()
			
						obbyDS:SetAsync(plr.UserId .. "-obbyStageProgress", plr.leaderstats.Stage.Value)
					end)
				end
			end
		end)
	end)	
end)

the comment in the first comment says why it does not work.

humanoid.Touched doesn’t exist

then what should i do and i should i add it in to the first script

You should use the .Touched event on every checkpoint. Saving the stage everytime the player hits one can produce Data Loss

then how would i make it so they spawn at the checkpoint when they join and die

U should only save when the Player leaves

yeh but how should i make so they spawn at they checkpoint when they die.

U can use Player.RespawnLocation to change at which Spawnpoint they spawn on

1 Like