Player wins don't increase when they win a game

Their data saves fine, but their amount of wins won’t change when they win.

local winsLeaderboard = game:GetService("DataStoreService"):GetDataStore("Wins")
local winner = contestantsTeam:GetPlayers()[1]
local currentWins = winsLeaderboard:GetAsync(winner.UserId) or 0
winsLeaderboard:SetAsync(winner.UserId, currentWins + 1)	
3 Likes

What do you mean it doesn’t change? The amount of wins in the data or perhaps on their leaderstats?

1 Like

By the way, you shouldn’t be saving every time a value is changed. You should be saving when the player leaves the game.

3 Likes

Neither of them change. It just moves on without any errors

1 Like

Do you want to give every player in the contestant’s team a win? Your current code only saves a win to one player in the contestant’s team.

1 Like

No, it’s only supposed to be the last person on the team who gets a win

1 Like

Try debugging it. See whether the ‘winner’ exists or not. Tho I could be wrong since I haven’t used normal DataStore in a while

1 Like

It does, I have another line of code under it that announces who the winner is without any issues

screenText = winner.Name .. " has won the game!"
1 Like
local winsLeaderboard = game:GetService("DataStoreService"):GetDataStore("Wins")
local winner = contestantsTeam:GetPlayers()[1]
local currentWins = winsLeaderboard:GetAsync(winner.UserId) or 0
winsLeaderboard:SetAsync(winner.UserId, currentWins += 1)	
1 Like

It’s just giving me errors and stopped the script from running

1 Like

can you send a screenshot of the errors that come up?

1 Like


with the original script though, there’s no errors

1 Like

Could you send that line and surrounding lines as a code snippet?

1 Like
	if #queue == 0 then
		print("1")
		if #contestantsTeam:GetPlayers() == 1 then
			local winner = contestantsTeam:GetPlayers()[1]
			local currentWins = winsLeaderboard:GetAsync(winner.UserId) or 0
			winsLeaderboard:SetAsync(winner.UserId, currentWins += 1)	

			screenText = winner.Name .. " has won the game!"
			wait(.1)
			Remote:FireAllClients(screenText)
			for _, player in pairs(judgesTeam:GetPlayers()) do
				player.Character:MoveTo(lobbyTeleport.Position)
				wait(.01)
				player.Character:MoveTo(lobbyTeleport.Position)
			end
			wait(5)
			startGame()
			beginning = true
1 Like
local winsLeaderboard = game:GetService("DataStoreService"):GetDataStore("Wins")
local winner = contestantsTeam:GetPlayers()[1]
winsLeaderboard:IncrementAsync(winner.UserId, 1)	
Optimization
  • Like @Katrist said, you shouldn’t be modifying the datastore everytime. Ideally, you must be fetching user data upon joining and save it to some sort of a cache table. During the player’s time in the server, you must be modifying values in the cache table and then finally when the player leaves, save their data to the datastore.
2 Likes

You could try changing the line winsLeaderboard:SetAsync(winner.UserId, currentWins += 1). To fix this particular error, try changing it to

local updatedWins = currentWins + 1
winsLeaderboard:SetAsync(winner.UserId, updatedWins)

However, this is a very risky and inefficient piece of code. The DataStoreService is a network request, which means it can throw internal errors. You should make a wins IntValue within the player, and only save and load data on joining and leaving. Whenever accessing the data store, always use a pcall() to catch and handle any errors. An example of what I mean:

local dataStore = game:GetService("DataStoreService")
local store = dataStore:GetDataStore("") --data store here


game.Players.PlayerAdded:Connect(function(player)
    local dataLoadingFailed = Instance.new("BoolValue", player)
    local wins = Instance.new("IntValue", player) --the wins value
    local winsValue
    local success, err = pcall(function() --protected call - catch and handle errors
        winsValue = store:GetAsync(key) --change that to your data store/load key
    end)

    if success then --if no internal errors were thrown
        wins.Value = winsValue
    elseif not success and err then
        warn("Data loading failed")
        dataLoadingFailed.Value = true
        player:Kick("Failed to load data - rejoin please")
    end
end)
2 Likes

This loads the player’s data when they join. Then, whenever a win is granted, it will be added to the IntValue set up beforehand. The pcall() will catch and handle any internal errors, and the dataLoadingFailed variable would be used in the save() function to check if the data loading failed.

Only update the DataStore on joining and leaving, never mid-way through the game.

Thank you, and why shouldn’t you update mid-way through the game? I’ve seen a lot of games where they have timed data saved notifications or buttons to save your data, so I thought it was fine

1 Like

Yes, but the problem here is you are updating too often. Often people make an autosave just as a backup, but generally you should leave the main saving to joining and leaving.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.