Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

No errors in console, but wont save coins ingame?

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)

5 answers

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

0

Doesnt work, says bad argument on 7.

0

I fixed all my issues, my apologies.

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.

6 comments

0

Try my program one last time, ensure it is a ServerScript, preferably stored in ServerScriptService

0

script still isn't working, any other way to save my stats?

0

I resolved all my issues, I was just in the middle of debugging and you came a bit early! Everything should work just fine now!

0

no, its not working... if you want I could send you the rbxl if you gave me a way to send files

0

I'm working on fixing it, I'm finding it odd, it should be functioning properly.

0

Yo, @Lazerbrine8 , your game "Project Creatures" violates ROBLOX TOS, and will be deleted if it becomes popular. It's what happened to Project Pokemon

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.