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

Why isn't my DataStore working?I am using SetAsync and GetAsync and PlayerRemoving event

I know if it sounds stupid, but I have no idea why my datastore is not working. The first one works, to check if the user has been there, but the second one to save the cash is not working any ideas?

Player = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local PlayerData = DataStore:GetDataStore("PlayerData")
local PlayerCash = DataStore:GetDataStore("PlayerCash")

Player.PlayerAdded:connect(function(Player)
	local stat = Instance.new("IntValue", Player)
	stat.Name = "leaderstats"
	local Cash = Instance.new("IntValue", stat)
	Cash.Name = "Cash"
	
	
	local Old = PlayerData:GetAsync(Player.UserId)
	local OldCash = PlayerCash:GetAsync(Player.UserId)
	if OldCash then
		Cash.Value = PlayerCash:SetAsync(Player.UserId, Cash.Value)
	else
		PlayerCash:SetAsync(Player.UserId, Cash.Value)
	end
	if Old then
		print("The player has been here!")
	else
		print("Player hasn't been here!")
		PlayerData:SetAsync(Player.UserId, true)
		
	end
	
end)
Player.PlayerRemoving:connect(function(Player)
	print("Saving Data!")
	PlayerCash:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
end)

1 answer

You are setting the data where you should be getting the data XD, also you should use a table where possible as you can save more data with less requests. You currently make multiple request which can be combined into one table.

If the player has no data then nil is returned so you can use this to tell if a player has saved data. It is also good to use one data store per game unless you have a good reason to do so as you can change the key for saving data, e.g. backpack_, stats_ or info_ ect. The UserId is used to make the key unique.

Putting it all together using tables:-

local Player = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local PlayerData = DataStore:GetDataStore("PlayerData")

Player.PlayerAdded:connect(function(Player)
    local stat = Instance.new("IntValue", Player)
    stat.Name = "leaderstats"
    local Cash = Instance.new("IntValue", stat)
    Cash.Name = "Cash"

	-- it is a good idea to add a prefix as we could also do 'user_save5_'  ect for game slots
    local data= PlayerData:GetAsync('user_' .. tostring(Player.UserId))
    if data then
        Cash.Value =  data[1] or 0 -- access the data as a table, we use 'or' as it will return the second arg is the first is false or nil e.g. print(false or 'a') 
	print("The player has been here!")
    else
        -- most people save data here but it is not always useful
	 print("Player hasn't been here!")
    end

end)
Player.PlayerRemoving:connect(function(Player)
    print("Saving Data!")
    PlayerData :SetAsync('user_' .. tostring(Player.UserId), {Player.leaderstats.Cash.Value}) -- we save a table of data
end)

This is just a basic example and it does not cover any of the limitations / possible errors with a data store.

I hope this helps, please comment if you do not understand how / why this code works.

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