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

I can't save money data and I don't know why?

Asked by
sheepposu 561 Moderation Voter
5 years ago
Edited 5 years ago

I've stayed up all night trying to get this to work but it just wouldn't . The code makes sense to me, so I can't figure out why it won't work. The output says no errors, just returning a nil value for trying to find the player Id. Which means it is not properly saving my Id to the data bank.

Heres the code. I've labeled it. The code is in a regular script inside 'ServerScriptService'

--Creates the data Store as ds
local ds = game:GetService("DataStoreService"):GetDataStore("-xBuckDatax-")

game.Players.PlayerAdded:Connect(function(player)
    -- saves the player Id as key
    local key = "Player_"..player.UserId
    local folder = Instance.new("Folder",player)
    folder.Name = "leaderstats"
    local money = Instance.new("IntValue",folder)
    money.Name = 'ObbyBucks<|>'
    local moneyalready
    --moneyalready is the id of the player
    local success, err = pcall(function()
        moneyalready.Value = ds:GetAsync(key)
    end)
    -- if it found the players Id, It made money equivilent to what was saved in the data store 
    if success then
        money.Value = moneyalready
    else
        --If not, money is equivilent to 100 and it saves that to the data store
        money.Value = 100
        ds:SetAsync(key, 100)
    end
end)

-- when someone is leaving...
game.Players.PlayerRemoving:Connect(function(player)
    local key = "Player_"..player.UserId
    --saves their key and money to the data store
    ds:SetAsync(key, player.leaderstats["ObbyBucks<|>"].Value)
end)
0
I noticed on line 14 that you have the Value you want to save indexed in the get async parameters you just need to Put the key Inside and leave out the "BlahBlah.Value" so: DataStore:GetAsync(Key) Cyrakohl 108 — 5y

1 answer

Log in to vote
1
Answered by
zblox164 531 Moderation Voter
5 years ago
Edited 5 years ago

You should save using tables. This will make it easier in the future to add other values too. I fixed up your code below but keep in mind this is untested.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("-xBuckDatax-")

-- Making the saving inside a function it shortened your code by a bit.
local function Save(player)
    local key = "Player_"..player.UserId

    local save = {
        ["ObbyBucks"] = player.leaderstats["ObbyBucks<|>"].Value
    }

    --saves their key and money to the data store
    local success, err = pcall(function()
        DataStore:SetAsync(key, save)
    end)

    if not success then
        warn("Failed to over-write data"..tostring(err))
        return
    end
end

-- I made this a function so it can be called whenever
local function Load(player)
    -- saves the player Id as key
    local key = player.UserId

    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = player -- setting the parent in the .new is deprecated

    local money = Instance.new("IntValue")
    money.Name = "ObbyBucks<|>"
    money.Parent = folder

    local moneyAlready

    local success, err = pcall(function()
        moneyAlready.Value = DataStore:GetAsync(key)
    end)

    if not success then
        warn("Failed to read data"..tostring(err))
        return
    end

    if moneyAlready then
        money.Value = moneyAlready.ObbyBucks
    else
        Save(player)
    end
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

All I did was improve the code.

What I did:

Split the DataStore variables, made the saving and loading in a function that you control when it's called, editing the save and load system, removed deprecated code.

More information on DataStores

0
So the past 40 min I have been testing it and looking over it and finally I've come out with it not working. This script is great. Thankyou. I posted a vid of me testing it out right here - https://youtu.be/VB7j4XKfXgM You may be able to find the prob. sheepposu 561 — 5y
0
Im going to assume he solved your problem from that comment alone so I'll accept the answer on your behalf. Next time accept the answer that helped you the most. User#24403 69 — 5y
0
It didn't solve my problem tho sheepposu 561 — 5y
0
Do you have data stores enabled? zblox164 531 — 5y
Ad

Answer this question