This is because you accidentally supplied a third argument to :GetAsync() because of a comma, I believe you meant to use a period. This will fail because a third argument, does not exist. :GetAsync() requires the key used to store the data, and the data you wish to be stored.
Note:
The secondary parental argument of Instance.new() is deprecated, meaning it is no longer supported as a practice by Lua. Please set the Parent property outside of this constructor.
It is also suggested that you wrap :GetAsync() calls and :SetAsync calls in a pcall scope to prevent these methods from potentially raising exceptions and canceling the thread; causing the program to stop and error.
Another final suggestion is to use :GetPropertyChangedSignal instead of the .Changed event. This will allow you to hone into a property you with to listen to, against .Changed firing at any modification; situations like so may include the ValueObjects name, or sudden changes outside the Value can be made which can exhaust the DataStoreService API if asked to save too many times.
Lastly, I formatted your program to operate with function to make it more efficient.
local DataStore = game:GetService("DataStoreService")
local CoinsSave = DataStore:GetDataStore("CoinsSaveSystem")
local Players = game:GetService("Players")
local function GetSaveData(Player)
if not (Player and Player:IsA("Player")) then return end
local Coins;
local Success = pcall(function()
Coins = CoinsSave:GetAsync(Player.UserId)
end)
if (Success and Coins) then
return Coins.Value
end
return nil
end
local function SaveData(Player)
if not (Player and Player:IsA("Player")) then return end
local Leaderstats = Player:FindFirstChild("leaderstats",1)
local Coins = Leaderstats:FindFirstChild("Coins",1)
local _,Unsuccessful = pcall(function()
CoinsSave:SetAsync(Player.UserId, Coins.Value)
end)
if (Unsuccessful) then warn(Unsuccessful) end
end
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder"); Leaderstats.Parent = Player
Leaderstats .Name = "leaderstats"
local Coins = Instance.new("IntValue"); Coins.Parent = Leaderstats
Coins.Name = "Coins"
Coins.Value = GetSaveData(Player) or 0
Coins:GetPropertyChangedSignal("Value"):Connect(function()
SaveData(Player)
end)
end)
Players.PlayerRemoving:Connect(SaveData)
2 comments
Doesnt work, says bad argument on 7.
I fixed all my issues, my apologies.