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

Datastore loads, but doesn't save?

I was working on a multiple stat saving system for my game, and this is what it turned out:

local datastore = game:GetService("DataStoreService"):GetDataStore("BronzeDarkRPstats")
game:GetService("Players").PlayerAdded:Connect(function(plr)
	local playerGui = plr.PlayerGui
	local cash = Instance.new("IntValue")
	cash.Parent = plr
	cash.Name = 'Cash'
	cash.Value = 10000
	local karma = Instance.new("IntValue")
	karma.Parent = plr
	karma.Name = 'Karma'
	local printersPurchased = Instance.new("IntValue")
	printersPurchased.Parent = plr
	printersPurchased.Name = "PrintersPurchased"
	local job = Instance.new("StringValue")
	job.Parent = plr
	job.Name = "Job"
	local payment = Instance.new("IntValue")
	payment.Parent = plr
	payment.Name = "Payment"
	
	plr.CharacterAdded:Connect(function(char)
		local flagged = Instance.new("BoolValue")
		flagged.Parent = char
		flagged.Name = "Flagged"
		flagged.Value = false
		local nametag = game.ReplicatedStorage.Misc.Nametag:Clone()
		nametag.Parent = char:WaitForChild("Head")
		nametag.Person.Text = plr.Name
		job.Changed:Connect(function(val)
			nametag.Job.Text = "["..val.."]"
		end)
		flagged.Changed:Connect(function(val)
			if val == true then
				nametag.Person.TextColor3 = Color3.fromRGB(255,0,0)
			elseif val == false then
				nametag.Person.TextColor3 = Color3.fromRGB(255,255,255)
			end
		end)
	end)
	
	local data
	
	local success,failure = pcall(function()
		data = datastore:GetAsync(plr.UserId)
	end)
	
	if success and data then
		plr.Cash.Value = data.cash
		plr.Karma.Value = data.karma
		print("successfully loaded data")
	elseif failure and not success then
		print(failure)
		warn('data did not load')
	end
end)
game:GetService("Players").PlayerRemoving:Connect(function(plr)
	for i,v in pairs(workspace:GetDescendants()) do
		if v.Name == plr.Name.."_Item" then
			v:Destroy()
		end
	end
	local playerGui = plr:WaitForChild("PlayerGui")
	local dataTable = {}
	dataTable.cash = plr.Cash.Value
	dataTable.karma = plr.Karma.Value
	local success,failure = pcall(function()
		datastore:SetAsync(plr.UserId,dataTable)
	end)
	if success then
		print('data saved')
	elseif failure and not success then
		print(failure)
		warn('data failed to save')
	end
end)

However, the output does not display the "data saved", leading me to believe that saving did NOT save.

Did I do something wrong?

1 comment

0

Try using clonetrooper's datastore editor to see the values.

1 answer

✓ Accepted answer

Try using :BindToClose. I used to have the same problem where I got no errors in my script, everything had loaded, but my data didn’t save. This could happen when you’re the last player in the server or you’re playing solo. The server can shut down before your remaining functions have run. Simply just copy and paste your function for PlayerRemoving in :BindToClose at the end of your script. Do not remove anything from your original script unless you’re trying to fix a mistake.


local datastore = game:GetService("DataStoreService"):GetDataStore("BronzeDarkRPstats")
game:GetService("Players").PlayerAdded:Connect(function(plr)
    local playerGui = plr.PlayerGui
    local cash = Instance.new("IntValue")
    cash.Parent = plr
    cash.Name = 'Cash'
    cash.Value = 10000
    local karma = Instance.new("IntValue")
    karma.Parent = plr
    karma.Name = 'Karma'
    local printersPurchased = Instance.new("IntValue")
    printersPurchased.Parent = plr
    printersPurchased.Name = "PrintersPurchased"
    local job = Instance.new("StringValue")
    job.Parent = plr
    job.Name = "Job"
    local payment = Instance.new("IntValue")
    payment.Parent = plr
    payment.Name = "Payment"

    plr.CharacterAdded:Connect(function(char)
        local flagged = Instance.new("BoolValue")
        flagged.Parent = char
        flagged.Name = "Flagged"
        flagged.Value = false
        local nametag = game.ReplicatedStorage.Misc.Nametag:Clone()
        nametag.Parent = char:WaitForChild("Head")
        nametag.Person.Text = plr.Name
        job.Changed:Connect(function(val)
            nametag.Job.Text = "["..val.."]"
        end)
        flagged.Changed:Connect(function(val)
            if val == true then
                nametag.Person.TextColor3 = Color3.fromRGB(255,0,0)
            elseif val == false then
                nametag.Person.TextColor3 = Color3.fromRGB(255,255,255)
            end
        end)
    end)

    local data

    local success,failure = pcall(function()
        data = datastore:GetAsync(plr.UserId)
    end)

    if success and data then
        plr.Cash.Value = data.cash
        plr.Karma.Value = data.karma
        print("successfully loaded data")
    elseif failure and not success then
        print(failure)
        warn('data did not load')
    end
end)
game:GetService("Players").PlayerRemoving:Connect(function(plr)
    for i,v in pairs(workspace:GetDescendants()) do
        if v.Name == plr.Name.."_Item" then
            v:Destroy()
        end
    end
    local playerGui = plr:WaitForChild("PlayerGui")
    local dataTable = {}
    dataTable.cash = plr.Cash.Value
    dataTable.karma = plr.Karma.Value
    local success,failure = pcall(function()
        datastore:SetAsync(plr.UserId,dataTable)
    end)
    if success then
        print('data saved')
    elseif failure and not success then
        print(failure)
        warn('data failed to save')
    end
end)

game:BindToClose(function()
    for _, plr in pairs(game.Players:GetPlayers()) do
        for i,v in pairs(workspace:GetDescendants()) do
        if v.Name == plr.Name.."_Item" then
            v:Destroy()
        end
    end
    local playerGui = plr:WaitForChild("PlayerGui")
    local dataTable = {}
    dataTable.cash = plr.Cash.Value
    dataTable.karma = plr.Karma.Value
    local success,failure = pcall(function()
        datastore:SetAsync(plr.UserId,dataTable)
    end)
    if success then
        print('data saved')
    elseif failure and not success then
        print(failure)
        warn('data failed to save')
    end
end)

Notice how instead of having the function run when PlayerRemoving is triggered, we loop through all the players in the server. :BindToClose will make all bound functions run 30 seconds before the server shuts down, regardless of how many players are still there.

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