Incrementing to a data store breaks my game loop

when i try to reward a winning team for winning a game by adding money to each player when they win, it completely breaks my game loop by having the countdown timer stuck at 60 seconds

code:

local Countdown = 60

		for i = Countdown, 0, -1 do
			RoundStatus.Value = "Game In Progress: "..i
			
			local survivors = game.Teams.Survivor
			
			local JugPlayerKey = "Player_" .. Juggernaut.UserId
			local SurPlayerKey = "Player_" .. survivors:GetPlayers()
			
			local Juggernaut = game.Teams.Juggernaut
				if #Juggernaut:GetPlayers() == 0 then
				RoundStatus.Value = "The Survivors have won!"
				CashDataStore:IncrementAsync(SurPlayerKey, #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(JugPlayerKey, #game.Players:GetPlayers() * 10)
				wait(5)
				break
				end
			
			wait(1)
		end

I think you’re using SurPlayerKey wrong.

		local SurPlayerKey = "Player_" .. survivors:GetPlayers() -- this is a array not a specific player
		
		local Juggernaut = game.Teams.Juggernaut
			if #Juggernaut:GetPlayers() == 0 then
			RoundStatus.Value = "The Survivors have won!"
			CashDataStore:IncrementAsync(SurPlayerKey, #game.Players:GetPlayers() * 10) -- you should use a for loop instead
			wait(5)
			break
			end

It could be something else though

1 Like

Also you are saving data in the loop for counting down. This means that if the data is taking a long time to save it will slow down or even stop the countdown.

The other thing is that it will completely stop as you are trying to save data to an array. This wont work. It should have given you a error message from this:

local SurPlayerKey = "Player_" .. survivors:GetPlayers()
CashDataStore:IncrementAsync(SurPlayerKey, #game.Players:GetPlayers() * 10)

#Game.Players:GetPlayers() will be a number, so 10 wont be a issue. Its the name of the store.

When saving data you would want to do:

for i = 1,#Players do
    CashDataStore:IncrementAsync(Players[i].UserId, #game.Players:GetPlayers() * 10)
end

This would then save to every one of the players rather than trying to save to a table id which would not work.

When saving data like this while a countdown is happening I would recommend spawning a coroutine so that the countdown isn’t effected. The other thing you would want to add is a pcall() for saving to the store as that will catch errors and also try again if it fails to save data.

1 Like

how would i be able to implement coroutine without breaking my script more? sorry im not very experienced in scripting yet

This should be about right, Please make sure you do some research into pcall() and coroutines as they are something that are useful to know.

So you would want to save you data as a function.
it would look something like this:

for i = 1,#Players do
    coroutine.wrap(function()
        while true do
        Success, Message = pcall(function()
            CashDataStore:IncrementAsync(Players[i].UserId, #game.Players:GetPlayers() * 10)
        end
        wait(3)
        if Success == nil then
            -- Retry or print Message
        else
            break --- Stops the loop
        end    
    end
    
end

Sources:

On a side note it would be useful to understand datastores properly.

1st , fetching the main store, the name of it will have a string.
2nd, the identifier in which data is saved to the main store will be string
3rd, the value saved

so overall when you are using datastores you can almost imagine the location.
StoreName:Identifier:Value

The identifier is usually set to something along the lines of the players user Id and Name.
Once you fully understand this later on you can do some crazy things with datastores.

2 Likes