Trying to increment to datastore but its breaking my game

trying to give ingame cash to the team who wins a round but when i add the datastore increment stuff it just breaks my game loop

code:

			RoundStatus.Value = "Game In Progress: "..i
			
				local player = game.Players.LocalPlayer
				local playerkey = "Player_" .. player.UserId
			
			local Juggernaut = game.Teams.Juggernaut
				if #Juggernaut:GetPlayers() == 0 then
				RoundStatus.Value = "The Survivors have won!"
				CashDataStore:IncrementAsync(playerkey, #game.Players:GetPlayers() * 10)
				wait(5)
				break
				end
			
			local Survivor = game.Teams.Survivor
				if #Survivor:GetPlayers() == 0 then
				RoundStatus.Value = "The Juggernaut has won!"
				CashDataStore:IncrementAsync(playerkey, #game.Players:GetPlayers() * 10)
				wait(5)
				break
				end
			
			wait(1)
		end

Would you mind posting the rest of the code block? Also, would you mind giving us a better description of how your game is breaking? If there are any error messages, those would be helpful as well.


Is this a Script ? Because server scripts can’t access LocalPlayer the way you are.

Alternatively, if this is a LocalScript you can’t access the DataStoreService .

well there are no errors in the console, the game countdown would just freeze at 60 seconds

here is my full script to make sure i give all the info i need to give:

ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")
teams = game:GetService("Teams")
DataStoreService = game:GetService("DataStoreService")
CashDataStore = DataStoreService:GetDataStore("cashDataStore")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
RoundStatus = ReplicatedStorage:WaitForChild('RoundStatus')

local Survivors = {}

local tool = game.ReplicatedStorage.Tools.Fists

while true do
	if #game.Players:GetPlayers() >= 2 then
		--Intermission

		local Countdown = 5

		repeat wait(1)
			Countdown = Countdown - 1

			RoundStatus.Value = 'Intermission : '..Countdown
		until Countdown <= 0

		--Choose the map

		RoundStatus.Value = 'Choosing map..'

		local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
		local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()

		wait(3)

		ChosenMap.Parent = workspace
		RoundStatus.Value = 'Teleporting players..'

		wait(1)

		--teleport the players
		
		
		
		local Players = game.Players:GetPlayers()

		local Juggernaut = Players[math.random(1, #Players)]
		
		if Juggernaut.Character ~= nil then
			Juggernaut.TeamColor = BrickColor.new("Persimmon")
			Juggernaut.Character.Humanoid.MaxHealth = 100 * #game.Players:GetPlayers() - 100
			Juggernaut.Character.Humanoid.Health = 100 * #game.Players:GetPlayers() - 100
			Juggernaut.Character.Humanoid.WalkSpeed = 20
		end
	

		for _, Player in pairs(game.Players:GetPlayers())do
			if Player.Character and Player.Character:FindFirstChild('Humanoid') then
				local RandomSpawn = Spawns[math.random(1, #Spawns)]
				Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				if Player ~= Juggernaut then
					Player.TeamColor = BrickColor.new("Storm blue")
					Player.Character.Humanoid.MaxHealth = 100
					Player.Character.Humanoid.Health = 100
					Player.Character.Humanoid.WalkSpeed = 16
					Survivors[Players] = true
				end
			
			
				local function GiveTool(player)
					local clone = tool:Clone()
					clone.Parent = Player.Backpack
				end
				GiveTool(Player)
			end
		end

		local Countdown = 60

		for i = Countdown, 0, -1 do
			RoundStatus.Value = "Game In Progress: "..i
			
				local player = game.Players.LocalPlayer
				local playerkey = "Player_" .. player.UserId
			
			local Juggernaut = game.Teams.Juggernaut
				if #Juggernaut:GetPlayers() == 0 then
				RoundStatus.Value = "The Survivors have won!"
				CashDataStore:IncrementAsync(playerkey, #game.Players:GetPlayers() * 10)
				wait(5)
				break
				end
			
			local Survivor = game.Teams.Survivor
				if #Survivor:GetPlayers() == 0 then
				RoundStatus.Value = "The Juggernaut has won!"
				CashDataStore:IncrementAsync(playerkey, #game.Players:GetPlayers() * 10)
				wait(5)
				break
				end
			
			wait(1)
		end

		--Kill the players

		for _, Player in pairs(game.Players:GetChildren())do
			if Player.Character and Player.Character:FindFirstChild('Humanoid') then
				Player.Character.Humanoid:TakeDamage(9999999999)
			end
		end

		ChosenMap:Destroy()

		RoundStatus.Value = 'Round over!'

		wait(1)
	else
		local dots = "..."
		
		repeat
			for i = 1,3 do
				RoundStatus.Value = "Waiting For Players"..string.sub(dots, 1, i)
				wait(1)
			end
		until #game.Players:GetPlayers() >= 2
	end

end

Is this a server or client script? As @RBX_Lua said, server scripts can not access LocalPlayer, and client scripts can not access DataStoreService.

ok so its a serverscript so i do i just have two playerkey variables and both with variables for each team instead of localplayer