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

Data isn't saving, but printing that it was loaded, no errors, anything wrong here?

Can somebody help me with a datastore script? I just recently started trying to learn it and it isn't working, API services is on and it prints "Data was loaded!", except it doesn't save when you leave for some reason, I don't know why it isn't saving

Here's the script:

local DSS = game:GetService("DataStoreService")
local Datastore = DSS:GetDataStore("Datastore-1")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"
	
	local data
	
	local success, errormessage = pcall(function()
		data = Datastore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
		print("Data was loaded!")
	else
		print("Error when getting data")
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		Datastore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("Data was not saved!")
		wait(errormessage)
	end
end)

Did I do anything wrong?

1 comment

0

ive got the same problem

1 answer

You want to save the value of the cash, not the object.

do this instead:

local DSS = game:GetService("DataStoreService")
local Datastore = DSS:GetDataStore("Datastore-1")

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

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

    local cash = Instance.new("IntValue", leaderstats)
    cash.Name = "Cash"

    local data

    local success, errormessage = pcall(function()
        data = Datastore:GetAsync(player.UserId.."-cash")
    end)

    if success then
        cash.Value = data
        print("Data was loaded!")
    else
        print("Error when getting data")
        warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
        Datastore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
    end)

    if success then
        print("Data successfully saved!")
    else
        print("Data was not saved!")
        wait(errormessage)
    end
end)

i hope this works for you!

1 comment

0

This didn't seem to work, still prints "Data was loaded!", but the cash is still at 0. Thanks for your answer though, I appreciate it

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