Hello, I was following a tutorial on the forum and i did a datastore script, but I would like to know how I can make my updateData function work well idk how to do it… could someone give me a hand?
In short, I need to be able to update the value of the coin, this with the help of a function, how could I do it? for a specific player (specific player name)
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerDataStore")
function updateData(player, newValue, ...)
return playerData:UpdateAsync(player, newValue)
end
function saveData(player, dateToStore, ...)
local table_To_Save = {
player.leaderstats.Money.Value,
player.leaderstats.Coins.Value
}
local success, errorMessage = pcall(function()
playerData:SetAsync(player.UserId, table_To_Save)
end)
if success then
warn('Data has been saved!')
else
print('DataStore error! Something went wrong.')
warn(errorMessage)
end
end
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local data
local success, errorMessage = pcall(function()
data = playerData:GetAsync(plr.UserId)
end)
if success then
Money.Value = data[1]
Coins.Value = data[2]
else
print("The player has no data!")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
saveData(plr) -- Data saved
end)
game:BindToClose(function() -- When server shuts down
for _, bindToClose_Player in pairs(game.Players:GetPlayers()) do
saveData(bindToClose_Player) -- Data saved
end
end)
the second value of UpdateAsync is a function that returns the new value
function updateData(player, newValue)
playerData:UpdateAsync(player, function(oldValue)
return newValue
end)
end
in this case, it is better to keep SetAsync, since it is faster and the other servers do not need to check that key
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerDataStore")
local OldData = {}
function saveData(player, dateToStore, ...)
local table_To_Save = {
player.leaderstats.Money.Value,
player.leaderstats.Coins.Value
}
local Old = OldData[player]
if not Old or (Old[1] == table_To_Save[1] and Old[2] == table_To_Save[2]) then return end
OldData[player] = table_To_Save
local success, message = pcall(playerData.SetAsync, playerData, player.UserId, table_To_Save)
if success then
warn('Data has been saved!')
else
print('DataStore error! Something went wrong.')
warn(message)
end
end
game:GetService("Players").PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local success, data = pcall(playerData.GetAsync, playerData, plr.UserId)
if success then
Money.Value = data[1] or 0
Coins.Value = data[2] or 0
else
print('DataStore error! Something went wrong.')
warn(data)
end
OldData[plr] = {Money.Value, Coins.Value}
end)
game:GetService("Players").PlayerRemoving:Connect(saveData) -- Data saved
game:BindToClose(function() -- When server shuts down
for _, bindToClose_Player in pairs(game.Players:GetPlayers()) do
task.spawn(saveData, bindToClose_Player) -- Data saved
end
end)
shorten it and add a table for history, so it is not saved 2 times.