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

Data store isn't saving data?

The data store script i wrote isn't saving data. Here is the code:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local PlayerData
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Money"
	cash.Parent = leaderstats
	
	local data
	local success, errormessage pcall(function()
		local Key = player.UserId
myDataStore:SetAsync(Key, PlayerData)
	end)
	
	if success then
		cash.Value = data
	else
		warn(errormessage)
	end
 end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage pcall(function()
		local PlayerData
		local Key = player.UserId
myDataStore:SetAsync(Key, PlayerData)
	end)
	
	if success == true then
		print("Data successfully saved!!!!!")
	else
		print("Data wasn'tt saved")
		warn(errormessage)
	end
		
	 end)

1 comment

0

Your code is very messy.

1 answer

✓ Accepted answer

If this doesn't work just comment and we can further debug.

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

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

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

    local cash = Instance.new("IntValue")
    cash.Name = "Money"
    cash.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        local Key = player.UserId
        data = myDataStore:GetAsync(Key, PlayerData)
    end)

    if success and data then
        cash.Value = data
    else
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        local PlayerData = player.leaderstats.Money.Value
        local Key = player.UserId
        myDataStore:SetAsync(Key, PlayerData)
    end)

    if success == true then
        print("Data successfully saved!")
    else
        print("Data wasn't saved")
        warn(errormessage)
    end

end)

game.BindToClose:Connect(function()
    -- Gives enough time to save data
    -- Makes studio wait 5 seconds before closing the game/server
    -- You can remove this if you want, just for studio testing purposes
    wait(5)
    print("Bind: All data should be saved")
end)
--[[
Changes:

    Added "=" to pcalls so you can see if it threw an error
    Set variable "PlayerData" to the players money value-
		you had it set to nothing so the server was saving blank
    Checked if the server got some data and not just success
    
]]

1 comment

0

Thanks. It worked

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