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)
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)
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
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.
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)
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
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.