Hi, I have a datastore script that saves both the coins that a player has, and their wins. It correctly saves their coins, however every time you join the game, the wins reset. Here is my script:
local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local WinStore = DataStoreService:GetDataStore("WinStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
Player.TeamColor = BrickColor.new("Really red")
local UserId = Player.UserId
local Currency = CurrencyStore:GetAsync(UserId)
if Currency == nil then
Currency = 0
CurrencyStore:SetAsync(UserId, Currency)
end
local Wins = WinStore:GetAsync(UserId)
if Wins == nil then
Wins = 0
WinStore:SetAsync(UserId, Wins)
end
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", Leaderstats)
Coins.Name = "Coins"
Coins.Value = Currency
local Wins = Instance.new("IntValue", Leaderstats)
Wins.Name = "Wins"
Wins.Value = Wins
Coins.Changed:Connect(function(NewValue)
CurrencyStore:SetAsync(UserId, NewValue)
end)
Wins.Changed:Connect(function(NewValue)
WinStore:SetAsync(UserId, NewValue)
end)
end)
Hi, instead of using SetAsync() every time the coins and wins change, you can save the data when the player leaves. You can also save the data in the same datastore but using a table. Also use DataModel | Roblox Creator Documentation to save the data before the game shutdown and be sure to test it on the roblox game client, not in roblox studio.
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
Players.PlayerAdded:Connect(function(Player)
Player.TeamColor = BrickColor.new("Really red")
local UserId = Player.UserId
local data
local success, err = pcall(function()
data = PlayerDataStore:GetAsync(UserId)
end)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", Leaderstats)
Coins.Name = "Coins"
local Wins = Instance.new("IntValue", Leaderstats)
Wins.Name = "Wins"
if success and data then
Coins.Value = data.Coins
Wins.Value = data.Wins
elseif not success then
warn(err)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local data = {
Coins = Player.leaderstats.Coins.Value,
Wins = Player.leaderstats.Wins.Value
}
local success, err = pcall(function()
PlayerDataStore:SetAsync(Player.UserId, data)
end)
if not success then
warn(err)
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
return
end
for _, player in pairs(Players:GetPlayers()) do
local data = {
Coins = player.leaderstats.Coins.Value,
Wins = player.leaderstats.Wins.Value
}
local success, err = pcall(function()
PlayerDataStore:SetAsync(player.UserId, data)
end)
if not success then
warn(err)
end
end
end)
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
Players.PlayerAdded:Connect(function(Player)
Player.TeamColor = BrickColor.new("Really red")
local UserId = Player.UserId
local data
local success, err = pcall(function()
data = PlayerDataStore:GetAsync(UserId)
end)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", Leaderstats)
Coins.Name = "Coins"
local Wins = Instance.new("IntValue", Leaderstats)
Wins.Name = "Wins"
if success and data then
Coins.Value = data.Coins
Wins.Value = data.Wins
elseif not success then
warn(err)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local data = {
Coins = Player.leaderstats.Coins.Value,
Wins = Player.leaderstats.Wins.Value
}
local success, err = pcall(function()
PlayerDataStore:SetAsync(Player.UserId, data)
end)
if not success then
warn(err)
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
wait(1)
end
for _, player in pairs(Players:GetPlayers()) do
local data = {
Coins = player.leaderstats.Coins.Value,
Wins = player.leaderstats.Wins.Value
}
local success, err = pcall(function()
PlayerDataStore:SetAsync(player.UserId, data)
end)
if not success then
warn(err)
end
end
end)
local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local WinStore = DataStoreService:GetDataStore("WinStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
Player.TeamColor = BrickColor.new("Really red")
local UserId = Player.UserId
local Currency = CurrencyStore:GetAsync(UserId)
if Currency == nil then
Currency = 0
CurrencyStore:SetAsync(UserId, Currency)
end
local Wins = WinStore:GetAsync(UserId)
if Wins == nil then
Wins = 0
WinStore:SetAsync(UserId, Wins)
end
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", Leaderstats)
Coins.Name = "Coins"
Coins.Value = Currency
local Wins = Instance.new("IntValue", Leaderstats)
Wins.Name = "Wins"
Wins.Value = Wins
Coins.Changed:Connect(function(NewValue)
CurrencyStore:SetAsync(UserId, NewValue)
end)
Wins.Changed:Connect(function(NewValue)
WinStore:SetAsync(UserId, NewValue)
end)
end)
while true do
wait(0.1)
for _, plr in pairs(game.Players:GetChildren()) do
if plr.TeamColor == BrickColor.new("Lime green") then
print(#game.Teams.Alive:GetPlayers())
if #game.Teams.Alive:GetPlayers() < 1.5 then
plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 100
wait(1.6)
end
end
end
end
(the other part of the script gives the pllayer coins and a win if they are the last one standing)