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

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

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)

1 comment

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)

1 answer

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

4 comments

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.

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.

0

It didn't solve my problem tho

0

Do you have data stores enabled?

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