function loadLeaderstats(L)local w=Instance.new("\073\110\116\086\097\108\117\101")w.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=w s.Value=0 local F=Instance.new("\073\110\116\086\097\108\117\101")F.Name="\083\099\111\114\101"F.Parent=w w.Parent=L end function initialiseRunStats(L)if L:FindFirstChild("\082\117\110\083\116\097\116\115")then L.RunStats.Distance.Value=0 L.RunStats.CoinsCollected.Value=0 end end function showResults(L)local w=game.ServerStorage.GUIs.PostRunGUI:Clone()w.Frame.DistanceValue.Text=L.RunStats.Distance.Value w.Frame.CoinsValue.Text=L.RunStats.CoinsCollected.Value w.Frame.ScoreValue.Text=L.leaderstats.Score.Value w.Parent=L.PlayerGui return w end function initialiseNewRun(L,w,s,F)if not path then while not path do wait()end end local m=nil if F then m=showResults(L)end if w~=0 then wait(w)end if m~=nil then m:Destroy()end if L and L.Parent then if L.Character or s==false then L:LoadCharacter()initialiseRunStats(L)local w=path()lastActivePath[L.Name]=w w:init(L.Name)end end end function setUpPostRunStats(L)local w=Instance.new("\070\111\108\100\101\114")w.Name="\082\117\110\083\116\097\116\115"w.Parent=L 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=w local F=Instance.new("\073\110\116\086\097\108\117\101")F.Name="\067\111\105\110\115\067\111\108\108\101\099\116\101\100"F.Value=0 F.Parent=w end function onPlayerEntered(L)L.CharacterAdded:connect(function(w)local s=w:WaitForChild("\072\117\109\097\110\111\105\100")if s then s.Died:connect(function()initialiseNewRun(L,3,true,true)end)end end)loadLeaderstats(L)setUpPostRunStats(L)initialiseNewRun(L,0,false,false)end game.Players.PlayerAdded:connect(onPlayerEntered)function onPlayerRemoving(L)local w=game.Workspace.Tracks:FindFirstChild(L.Name)if w~=nil then w:Destroy()end end game.Players.PlayerRemoving:connect(onPlayerRemoving)for L,w in pairs(game.Players:GetChildren())do onPlayerEntered(w)end

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, 3, 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


