function loadLeaderstats(W)local q=Instance.new("\073\110\116\086\097\108\117\101")q.Name="\108\101\097\100\101\114\115\116\097\116\115"local s=Instance.new("\073\110\116\086\097\108\117\101")s.Name="\072\105\103\104 \083\099\111\114\101"s.Parent=q s.Value=0 local h=Instance.new("\073\110\116\086\097\108\117\101")h.Name="\083\099\111\114\101"h.Parent=q q.Parent=W end function initialiseRunStats(W)if W:FindFirstChild("\082\117\110\083\116\097\116\115")then W.RunStats.Distance.Value=0 W.RunStats.CoinsCollected.Value=0 end end function showResults(W)local q=game.ServerStorage.GUIs.PostRunGUI:Clone()q.Frame.DistanceValue.Text=W.RunStats.Distance.Value q.Frame.CoinsValue.Text=W.RunStats.CoinsCollected.Value q.Frame.ScoreValue.Text=W.leaderstats.Score.Value q.Parent=W.PlayerGui return q end function initialiseNewRun(W,q,s,h)if not path then while not path do wait()end end local x=nil if h then x=showResults(W)end if q~=0 then wait(q)end if x~=nil then x:Destroy()end if W and W.Parent then if W.Character or s==false then W:LoadCharacter()initialiseRunStats(W)local q=path()lastActivePath[W.Name]=q q:init(W.Name)end end end function setUpPostRunStats(W)local q=Instance.new("\070\111\108\100\101\114")q.Name="\082\117\110\083\116\097\116\115"q.Parent=W local s=Instance.new("\073\110\116\086\097\108\117\101")s.Name="\068\105\115\116\097\110\099\101"s.Value=0 s.Parent=q local h=Instance.new("\073\110\116\086\097\108\117\101")h.Name="\067\111\105\110\115\067\111\108\108\101\099\116\101\100"h.Value=0 h.Parent=q end function onPlayerEntered(W)W.CharacterAdded:Connect(function(q)local s=q:WaitForChild("\072\117\109\097\110\111\105\100")if s then s.Died:Connect(function()initialiseNewRun(W,4,true,true)end)end end)loadLeaderstats(W)setUpPostRunStats(W)initialiseNewRun(W,0,false,false)end game.Players.PlayerAdded:Connect(onPlayerEntered)function onPlayerRemoving(W)local q=game.Workspace.Tracks:FindFirstChild(W.Name)if q~=nil then q:Destroy()end end game.Players.PlayerRemoving:Connect(onPlayerRemoving)for W,q in pairs(game.Players:GetChildren())do onPlayerEntered(q)end
-- Leaderboard
function loadLeaderstats(player)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local highScore = Instance.new("IntValue")
	highScore.Name = "High Score"
	highScore.Parent = stats
	highScore.Value = 0

	local currentScore = Instance.new("IntValue")
	currentScore.Name = "Score"
	currentScore.Parent = stats

	stats.Parent = player
end

function initialiseRunStats(player)
	if player:FindFirstChild("RunStats") then
		player.RunStats.Distance.Value = 0
		player.RunStats.CoinsCollected.Value = 0
	end
end

function showResults(player)
	local resultsGUI = game.ServerStorage.GUIs.PostRunGUI:Clone()
	resultsGUI.Frame.DistanceValue.Text = player.RunStats.Distance.Value
	resultsGUI.Frame.CoinsValue.Text = player.RunStats.CoinsCollected.Value
	resultsGUI.Frame.ScoreValue.Text = player.leaderstats.Score.Value

	resultsGUI.Parent = player.PlayerGui
	return resultsGUI
end

function initialiseNewRun(player, delayTime, charExpected, showLastResults)
	if not path then
		while not path do
			wait()
		end
	end

	local lastResultsGUI = nil
	if showLastResults then
		lastResultsGUI = showResults(player)
	end

	if delayTime ~= 0 then
		wait(delayTime)
	end

	if lastResultsGUI ~= nil then
		lastResultsGUI:Destroy()
	end

	if player and player.Parent then
		-- charExpected is needed to avoid calling LoadCharacter on players leaving the game
		if player.Character or charExpected == false then
			player:LoadCharacter()

			initialiseRunStats(player)

			local playersPath = path()
			lastActivePath[player.Name] = playersPath
			playersPath:init(player.Name)
		end
	end
end

function setUpPostRunStats(player)
	local folder = Instance.new("Folder")
	folder.Name = "RunStats"
	folder.Parent = player
	local currentDistance = Instance.new("IntValue")
	currentDistance.Name = "Distance"
	currentDistance.Value = 0
	currentDistance.Parent = folder
	local coinsCollected = Instance.new("IntValue")
	coinsCollected.Name = "CoinsCollected"
	coinsCollected.Value = 0
	coinsCollected.Parent = folder
end

function onPlayerEntered(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		if humanoid then
			humanoid.Died:Connect(function()
				initialiseNewRun(player, 4, true, true)
			end)
		end
	end)

	-- Initial loading
	loadLeaderstats(player)
	setUpPostRunStats(player)

	-- Start game
	initialiseNewRun(player, 0, false, false)
end
game.Players.PlayerAdded:Connect(onPlayerEntered)

function onPlayerRemoving(player)
	local track = game.Workspace.Tracks:FindFirstChild(player.Name)
	if track ~= nil then
		track:Destroy()
	end
end
game.Players.PlayerRemoving:Connect(onPlayerRemoving)

for _, player in pairs(game.Players:GetChildren()) do
	onPlayerEntered(player)
end
