Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

No errors in console, but wont save coins ingame?

Asked by 4 years ago
Edited by Ziffixture 4 years ago

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

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
0
Doesnt work, says bad argument on 7. TheHardKnockLite -5 — 4y
0
I fixed all my issues, my apologies. Ziffixture 6913 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)

Log in to vote
0
Answered by 4 years ago

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.

0
Try my program one last time, ensure it is a ServerScript, preferably stored in ServerScriptService Ziffixture 6913 — 4y
0
script still isn't working, any other way to save my stats? Lazerbrine8 2 — 4y
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! Ziffixture 6913 — 4y
0
no, its not working... if you want I could send you the rbxl if you gave me a way to send files Lazerbrine8 2 — 4y
View all comments (2 more)
0
I'm working on fixing it, I'm finding it odd, it should be functioning properly. Ziffixture 6913 — 4y
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 Void_Frost 571 — 3y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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)
Log in to vote
-1
Answered by 4 years ago

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)

Answer this question