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

Datastore not saving, what or how do i do to fix it?

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player


local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats

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

if success then
	cash.Value = data
else
	print("There was an error while getting your data")
	warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
	data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.points.Value)
	end)
	if success then
	print("Player Data successfully saved!")
else
	print("There was an error when saving your data")
	warn(errormessage)
	end
	
end)



	
	

Thanks for helping!

5 comments

0

Did you define "myDataStore"?

0

I dont think so, should i do local myDataStore = data?

0

Please show your ENTIRE script, thanks.

0

this is the entire script

0

Ohh.

2 answers

Since this is the entire script, you simply forgot to add the datastore variable.

Here is what you need to add!

local DSS = game:GetService("DataStoreService")
local myDataStore = DSS:GetDataStore("myDataStore")

Simply add this to the top of your script and it should work.

1 comment

0

^^^ Should solve your problem.

I'll just add to ParticularlyPenguin's answer. A lot of people also have this problem and it's that if you're playing solo in Roblox Studio, or you could also be the last player in a server, or the server could just shut down unexpectedly, but when you leave, the server may shut down before your functions such as PlayerRemoving finishes. If that function does not run then your player's data will not be saved. This is why it is always best to use a function called :BindToClose for saving data when a player leaves. It will allow all bound functions to run before the server shuts down. I've edited your script and you will see how it works. Lastly, on line 27, you did not capitalize "Points" in "player.leaderstats.Points.Value". "points" is not a valid member of leaderstats so it's important that you have the capitalization right.

local DSS = game:GetService("DataStoreService")
local myDataStore = DSS:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player


local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats

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

if success then
    cash.Value = data
else
    print("There was an error while getting your data")
    warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
    data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
    end)
    if success then
    print("Player Data successfully saved!")
else
    print("There was an error when saving your data")
    warn(errormessage)
    end

end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do -- loop through all the players in the server so every player's data is saved
		local success, errormessage = pcall(function()
			data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
    		end)
		if success then
			print("Player Data successfully saved!")
		else
		    	print("There was an error when saving your data")
		    	warn(errormessage)
		end
	end
end)

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