CCCPaste

New Wins Leaderboard Lobby Game

local DatastoreService = game:GetService("DataStoreService")
local TeleportService = game:GetService("TeleportService")
local dVersion = 1
local save = DatastoreService:GetDataStore("Wins")

game.Players.PlayerAdded:Connect(function(player)
	local previousData = save:GetAsync(player.UserId) -- Returns a number value.

	local Wins

	if previousData ~= nil then
		Wins = previousData
	else
		Wins = 0
		save:SetAsync(player.UserId, 0)
	end

	local coinsValue = Instance.new("IntValue", player)
	coinsValue.Name = "Wins"
	coinsValue.Value = Wins
	
	local Role = Instance.new("StringValue", player)
	Role.Name = "Role"
	Role.Value = player
	
	if player.Wins.Value >= 5 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2142461642)
	end
	if player.Wins.Value >= 10 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2142461711)
	end
	if player.Wins.Value >= 15 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2142514030)
	end
	if player.Wins.Value >= 20 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2142514057)
	end
	if player.Wins.Value >= 100 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2142514061)
	end
	if player.Wins.Value >= 1000 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2142514085)
	end
	if player.Wins.Value >= 10000 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2142514181)
	end
end)

game:BindToClose(function() -- Runs whenver the server is about to shut down/stop.
	print("STOPPED!")

	for i,player in pairs(game.Players:GetPlayers()) do
		local value = player.Wins.Value
		save:SetAsync(player.UserId, value)
		print("Saved data for "..player.Name)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local value = player.Wins.Value

	if value ~= nil then
		print("Found data to save for "..player.Name.."!")
		save:SetAsync(player.UserId, value)
		print("Saved data for "..player.Name)
	else
		print("Did not manage to find data to save for "..player.Name.."!")
	end
end)

local save2 = DatastoreService:GetDataStore("Rounds")

game.Players.PlayerAdded:Connect(function(player)
	local previousData = save2:GetAsync(player.UserId) -- Returns a number value.

	local Rounds

	if previousData ~= nil then
		Rounds = previousData
	else
		Rounds = 0
		save2:SetAsync(player.UserId, 0)
	end

	local coinsValue = Instance.new("IntValue", player)
	coinsValue.Name = "Rounds"
	coinsValue.Value = Rounds
end)

game:BindToClose(function() -- Runs whenver the server is about to shut down/stop.
	print("STOPPED!")

	for i,player in pairs(game.Players:GetPlayers()) do
		local value = player.Rounds.Value
		save2:SetAsync(player.UserId, value)
		print("Saved data for "..player.Name)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local value = player.Rounds.Value

	if value ~= nil then
		print("Found data to save for "..player.Name.."!")
		save2:SetAsync(player.UserId, value)
		print("Saved data for "..player.Name)
	else
		print("Did not manage to find data to save for "..player.Name.."!")
	end
end)
local CoinsSave = DatastoreService:GetDataStore("Coins")

game.Players.PlayerAdded:Connect(function(player)
	local previousData = CoinsSave:GetAsync(player.UserId) -- Returns a number value.

	local Coins

	if previousData ~= nil then
		Coins = previousData
	else
		Coins = 0
		CoinsSave:SetAsync(player.UserId, 0)
	end

	local coinsValue = Instance.new("IntValue", player)
	coinsValue.Name = "Coins"
	coinsValue.Value = Coins

	local val1 = Instance.new("StringValue",player)
	val1.Name = 'GotPet'
	val1.Value = ''

	local val2 = Instance.new("StringValue",player)
	val2.Name = 'OpenValue'
	val2.Value = ''
	
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 4481523680) then
		player.Coins.Value = player.Coins.Value * 2
	end
end)

game:BindToClose(function() -- Runs whenver the server is about to shut down/stop.
	print("STOPPED!")

	for i,player in pairs(game.Players:GetPlayers()) do
		local value = player.Coins.Value
		CoinsSave:SetAsync(player.UserId, value)
		print("Saved data for "..player.Name)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local value = player.Coins.Value

	if value ~= nil then
		print("Found data to save for "..player.Name.."!")
		CoinsSave:SetAsync(player.UserId, value)
		print("Saved data for "..player.Name)
	else
		print("Did not manage to find data to save for "..player.Name.."!")
	end
end)

local teleportData = TeleportService:GetLocalPlayerTeleportData()

if teleportData then
	local Player = game.Players:GetPlayerByUserId(teleportData.PlayerName)
	Player:WaitForChild('Wins').Value = Player.Wins.Value + 1
end