I'm trying to make a coin saving script, but it isn't working.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CashSaveSystem") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "leaderstats" local Cash = Instance.new("IntValue",leader) Cash.Name = "Coins" Cash.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, Cash.Value) Cash.Changed:connect(function() ds:SetAsync(player.UserId, Cash.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player,leaderstats.Cash.Value) end)
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.
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)
Something I found out was that when using the Instance.new() function, using the parenting part of it doesn't allow you to rename the instance, as with a few other properties. Not sure why it does that, but I fixed it for you.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CashSaveSystem") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder") leader.Parent = player leader.Name = "leaderstats" local Cash = Instance.new("IntValue") Cash.Parent = leader Cash.Name = "Coins" Cash.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, Cash.Value) Cash.Changed:connect(function() ds:SetAsync(player.UserId, Cash.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player,leaderstats.Cash.Value) end)
Yeah, none of these suggestions work. It wont save the coins and I am unable to make the game function properly without being able to save. If there's any other way to save a value, or you know the problem, please tell me immediately.
Don't know if this is important but... The script is in workspace.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CoinsSaveSystem") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "leaderstats" local Coins = Instance.new("IntValue",leader) Coins.Name = "Coins" Coins.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, Coins.Value) Coins.Changed:connect(function() ds:SetAsync(player.UserId, Coins.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player,leaderstats.Coins.Value) end)
If Its a different type of cash,rename the Coins anything. If you want 2 or more leaderstats just copy and paste like this
local DataStore = game:GetService("DataStoreService") local ds1 = DataStore:GetDataStore("CoinsSaveSystem") local ds2= DataStore:GetDataStore("GemsSaveSystem") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "leaderstats" local Coins = Instance.new("IntValue",leader) Coins.Name = "Coins" local Gems = Instance.new("IntValue",leader) Gems.Name = "Gems" Coins.Value = ds:GetAsync(player.UserId) or 0 ds1:SetAsync(player.UserId, Coins.Value) Gems.Value = ds:GetAsync(player.UserId) or 0 ds2:SetAsync(player.UserId, Coins.Value) Coins.Changed:connect(function() ds1:SetAsync(player.UserId, Coins.Value) end) Gems.Changed:connect(function() ds2:SetAsync(player.UserId, Gems.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds1:SetAsync(player.UserId, player,leaderstats.Coins.Value) end) game.Players.PlayerRemoving:connect(function(player) ds2:SetAsync(player.UserId, player,leaderstats.Gems.Value) end)
You put a comma when I think you were trying to put a period.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CashSaveSystem") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "leaderstats" local Cash = Instance.new("IntValue",leader) Cash.Name = "Coins" Cash.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, Cash.Value) Cash.Changed:connect(function() ds:SetAsync(player.UserId, Cash.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player.leaderstats.Cash.Value) end)