function loadLeaderstats(Q)local m=Instance.new("\073\110\116\086\097\108\117\101")m.Name="\108\101\097\100\101\114\115\116\097\116\115"local H=Instance.new("\073\110\116\086\097\108\117\101")H.Name="\072\105\103\104 \083\099\111\114\101"H.Parent=m H.Value=0 local N=Instance.new("\073\110\116\086\097\108\117\101")N.Name="\083\099\111\114\101"N.Parent=m m.Parent=Q end function initialiseRunStats(Q)if Q:FindFirstChild("\082\117\110\083\116\097\116\115")then Q.RunStats.Distance.Value=0 Q.RunStats.CoinsCollected.Value=0 end end function showResults(Q)local m=game.ServerStorage.GUIs.PostRunGUI:Clone()m.Frame.DistanceValue.Text=Q.RunStats.Distance.Value m.Frame.CoinsValue.Text=Q.RunStats.CoinsCollected.Value m.Frame.ScoreValue.Text=Q.leaderstats.Score.Value m.Parent=Q.PlayerGui return m end function initialiseNewRun(Q,m,H,N)if not path then while not path do wait()end end local b=nil if N then b=showResults(Q)end if m~=0 then wait(m)end if b~=nil then b:Destroy()end if Q and Q.Parent then if Q.Character or H==false then Q:LoadCharacter()initialiseRunStats(Q)local m=path()lastActivePath[Q.Name]=m m:init(Q.Name)end end 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

