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