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

DataStore not saving currencies?

Asked by 3 years ago

Hello! I have tried to create a double-currency system, with two different kinds of currencies but in the same script. As of now, I attempted to make the first one work due to not being loaded when the game starts, even if it saves. If you could, I would also love if you helped me in making another currency in the same script, thank you.

local currencyName = "¥"
local DataStore = game:GetService("DataStoreService"):GetDataStore("MoneyDataStore")

game.Players.PlayerAdded:Connect(function(player)

    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = player

    local currency = Instance.new("IntValue")
    currency.Name = currencyName
    currency.Parent = folder

    local playerID = ""..player.UserId
    local savedData = nil

    local savedData = DataStore:GetAsync(playerID)

    if savedData ~= nil then
        print("loaded"..currency.Value)
        currency.Value = savedData
    else
        print("hi")
        currency.Value = 0
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerID = currencyName.."coin-"..player.UserId
    DataStore:SetAsync(playerID, player.leaderstats[currencyName].Value)
    print("saved "..player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick()
        end
    end

    wait(5)
end)

4 answers

Log in to vote
1
Answered by 3 years ago

although i cant help you with fixing that script i can give you another script that does the same thing, just change the names:

local DSS = game:GetService("DataStoreService")
local PlayerData = DSS:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(Player)
    local StringData = PlayerData:GetAsync(Player.UserId) or 0
    local DataSplit = string.split(StringData, "+")
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player
    local Coins = Instance.new("NumberValue")
    Coins.Parent = leaderstats
    Coins.Name = "Coins"
    Coins.Value = DataSplit[1] or 0
    local Multiplier = Instance.new("NumberValue")
    Multiplier.Parent = leaderstats
    Multiplier.Name = "Multiplier"
    Multiplier.Value = DataSplit[2] or 1
    local Rebirths = Instance.new("NumberValue")
    Rebirths.Parent = leaderstats
    Rebirths.Name = "Rebirths"
    Rebirths.Value = DataSplit[3] or 1
end)
game.Players.PlayerRemoving:Connect(function(Player)
    PlayerData:SetAsync(Player.UserId, Player.leaderstats.Coins.Value.. "+".. Player.leaderstats.Multiplier.Value.. "+".. Player.leaderstats.Rebirths.Value)
end)
Ad
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
3 years ago
Edited 3 years ago

Put this script in your stats script

local dataStores = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")
local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)

    playersLeft = playersLeft + 1

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats

    local wins = Instance.new("IntValue")
    wins.Name = "Wins"
    wins.Value = 0
    wins.Parent = leaderstats

    end)

    -- Data stores

    local player_data

    pcall(function()
        player_data = dataStores:GetAsync(player.UserId.."-Coins")
    end)

    if player_data ~= nil then
        -- Player has saved data load it in
        coins.Value = player_data
    else
        -- New player
    end

end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
        dataStores:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
        print("Saved!")
        playersLeft = playersLeft - 1
        bindableEvent:Fire()
    end)

end)

game:BindToClose(function()
    while playersLeft > 0 do
        bindableEvent.Event:Wait()
    end
end)

0
You forgot to create the two values to save, I think, lmao Fixer1987 95 — 3y
0
Unfortunately it doesn't seem to be working, I will answer to my post with the script I came up with. Fixer1987 95 — 3y
0
Unfortunately it doesn't seem to be working, I will answer to my post with the script I came up with. Fixer1987 95 — 3y
0
Put it into a Pcall function. KixWater 126 — 3y
Log in to vote
0
Answered by 3 years ago

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData") game.Players.PlayerAdded:Connect(function(plr) wait() local folder = Instance.new("Folder") folder.Name = "leaderstats" folder.Parent = plr local currency = Instance.new("IntValue") currency.Name = "YEN" currency.Parent = plr.leaderstats local currency = Instance.new("IntValue") currency.Name = "MON" currency.Parent = plr.leaderstats local plrkey = "id_"..plr.UserId local save1 = plr.leaderstats.YEN local save2 = plr.leaderstats.MON local GetSaved = ds:GetAsync(plrkey) if GetSaved then save1.Value = GetSaved[1] save2.Value = GetSaved[2] else local NumberForSaving = {save1.Value, save2.Value} ds:GetAsync(plrkey, NumberForSaving) end end) game.Players.PlayerRemoving:Connect(function(plr) ds:SetAsync("id_"..plr.userId, {plr.leaderstats.YEN.Value, plr.leaderstats.MON.Value}) end)
Log in to vote
0
Answered by 3 years ago

Hmm idk if this is the cause but in lines 15 and 17 you're putting local for both, it should be phrased as

local savedData -- you can put = nil here
savedData = DataStore:GetAsync(playerID)

that's all I have right now..

Answer this question