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

Why is datastore not saving data?

The title basicly explains it all.

My datastore isn't working correctly. I have no idea why.

Here is my datastore script.

local DSService = game:GetService('DataStoreService'):GetDataStore('Leaderstats2')
game.Players.PlayerAdded:Connect(function(plr)
	-- Define variables
	local uniquekey = 'id-'..plr.userId
	local leaderstats = Instance.new('IntValue', plr)
	leaderstats.Name = 'leaderstats'
	
	local savevalue =  Instance.new('IntValue')
	savevalue.Parent = leaderstats
	savevalue.Name = 'Money'
	savevalue.Value = '500'
	
	local savevalue2 =  Instance.new('IntValue')
	savevalue2.Parent = leaderstats
	savevalue2.Name = 'Gems'
	
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
		savevalue2.Value = GetSaved[2]
	else
		local NumbersForSaving = {savevalue.Value, savevalue2.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Money.Value, plr.leaderstats.Gems.Value}
DSService:SetAsync(uniquekey, Savetable)	
	
	
end)

It's in a script in serverscripts.

It works correctly, but when you leave and join back none of the data is saved. It's the exact same data script as my other game, but it works on my other game perfectly.

Also when I print my datastore in developer console.

The value it shows 0.

https://imgur.com/8l5Nbnp

I know that it has changed.

AND YES I DO KNOW THAT IN THAT IMAGE A PRINT A DIFFERENT DATA STORE!!!

The same thing happens for the money.

1 answer

GENERAL PRACTICE

Use :GetService() to retrieve the Players Service

Define the parent outside of the Instance.new() function

ISSUES

The Money.Value = "500" line should just be the number 500 since the value is not a string.

The player's id property is UserId, not "userId" (lowercase u). The script will not work since Lua is Case Sensitive, so since the value is not the correct spelling, it is invalid.

As a result, change lines with

plr.userId

to

plr.UserId

REVISED SCRIPT

local DSService = game:GetService("DataStoreService"):GetDataStore("Leaderstats2")

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local uniquekey = ("id-"..plr.UserId)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local savevalue = Instance.new("IntValue")
	savevalue.Name = "Money"
	savevalue.Value = 500
	savevalue.Parent = leaderstats

	local savevalue2 = Instance.new("IntValue")
	savevalue2.Name = "Gems"
	savevalue2.Value = 0
	savevalue2.Parent = leaderstats

	local success, result = pcall(function()
		DSService:GetAsync(uniquekey)
	end)

	if not success then
		warn(result)
	else
		local GetSaved = DSService:GetAsync(uniquekey)
		if GetSaved ~= nil then
			savevalue.Value = GetSaved[1]
			savevalue2.Value = GetSaved[2]
		end
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
	local uniquekey = ("id-"..plr.UserId)
	local Savetable = {plr.leaderstats.Money.Value, plr.leaderstats.Gems.Value}
	DSService:SetAsync(uniquekey, Savetable)
end)

EDIT: I've added a pcall which will make sure that there is no error when loading the data and removed the setasync in the loading function (this is not required since the data will save when the player leaves)

Please note without API Services Enabled in Studio the Data will not save if you test it in Studio, but will work in an actual server

3 comments

0

Thanks. Doesn't work, but thats for helping!

0

Please try the edited revision, i added a pcall so that it will determine if an error is thrown and removed a redundant section

0

@kittonlover101 does it works in game and not in studio ? if yes then you should use :Kick() on yourself so the playerremoving event will fire

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