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

Player currency not saving in DataStore?

Hello again, I am using the DataStore service to save a player's money. There are no errors in the output, but the amount of cash I enter in the value doesn't save. Again, I am using AlvinBlox's tutorial. Here is the entire code:

local CurrencyName = "C01nz"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")

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 ID = CurrencyName.."="..player.UserId
    local SavedData = nil

    pcall(function()
        SavedData = DataStore:GetAsync(ID)
    end)
    
    if SavedData ~= nil then
	   Currency.Value = SavedData
        print("Data loaded.")
    else 
	-- New Player1!!111
	   Currency.Value = 0 
       print("A new player")
   end
    
end)


game.Players.PlayerRemoving:Connect(function(player)
    local ID = CurrencyName..":"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[CurrencyName].Value)
    print("Data Saved")

end)

game:BindToClose(function()
	
    --When game is ready to shutdown
    for i, player in pairs(game.Players:GetPlayers()) do
	   if player then
		player:Kick("This game is shutting down.")
		end
	end
    wait(5)

end)

There are no errors in the output, and I looked at the tutorial numerous times, so I am really stumped. Help would be appreciated.

1 answer

local CurrencyName = "C01nz"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local SavedData = DataStore:GetAsync(player.UserId)
		
	    local Folder = Instance.new("Folder")
	    Folder.Name = "leaderstats"
	    Folder.Parent = player
	
	    local Currency = Instance.new("NumberValue")
	    Currency.Name = CurrencyName
	    Currency.Parent = Folder
		Currency.Value = SavedData[CurrencyName] or false
	end)
end)


game.Players.PlayerRemoving:Connect(function(player)
	--Just incase if you add more values, I made u a table
    local data = {
[CurrencyName] = player:WaitForChild("leaderstats"):WaitForChild(CurrencyName).Value
}
	
	DataStore:SetAsync(player.UserId, data)
end)

game:BindToClose(function()

    --When game is ready to shutdown
    for i, player in pairs(game.Players:GetPlayers()) do
       if player then
        player:Kick("This game is shutting down.")
        end
    end
    wait(5)

end)

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