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

DataStore not loading properly?

I have a script that loads and saves a few boolvalues, but some of these values cannot be loaded properly for some reason. The pcall function always ends up failing and printing the error message in the output.

Here is my code:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "stats"
	stats.Parent = player
	
	local valuetwo = Instance.new("BoolValue")
	valuetwo.Name = "two"
	valuetwo.Parent = stats
	local valuethree = Instance.new("BoolValue")
	valuethree.Name = "three"
	valuethree.Parent = stats
	local valuefour = Instance.new("BoolValue")
	valuefour.Name = "four"
	valuefour.Parent = stats

	local data2
	local data3
	local data4
	local success, errormessage = pcall(function()
		data2 = myDataStore:GetAsync(player.UserId.."-two")
		data3 = myDataStore:GetAsync(player.UserId.."-three")
		data4 = myDataStore:GetAsync(player.UserId.."-four")
	end)

	if success then
		valuetwo.Value = data2
		valuethree.Value = data3
		valuefour.Value = data4
	else
		print("data failed to load")
		warn(errormessage)
	end
end)

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

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-two" , player.stats.two.Value)
		myDataStore:SetAsync(player.UserId.."-three" , player.stats.three.Value)
		myDataStore:SetAsync(player.UserId.."-four" , player.stats.four.Value)	
	end)

	if success then
		print("player data saved")
	else
		print("oh no data save failed")
		warn(errormessage)
	end
end)

Output: "data failed to load"

2 comments

0

Make sure you have Studio access to API services enabled.

0

Still doesn't work. The error message is no longer printed, but it doesn't actually save.

1 answer

i always check to see if it has data stored so i would change the line 22-27 to:

local success, errormessage = pcall(function()
	if myDataStore:GetAsync(player.UserId.."-two") ~= nil then
	data2 = myDataStore:GetAsync(player.UserId.."-two")
	end
	if myDataStore:GetAsync(player.UserId.."-three") ~= nil then
	data3 = myDataStore:GetAsync(player.UserId.."-three")
	end
	if myDataStore:GetAsync(player.UserId.."-four") ~= nil then
	data4 = myDataStore:GetAsync(player.UserId.."-four")
	end
end)

also if you are testing it from studio make sure studio access to api services is enabled, and then add to a script this line:

game:BindToClose(wait(5))

this will make it so it will wait 5 seconds inside roblox studio and it will have the time to save the data, because in a normal server it will still run for a few seconds but inside studio when you press stop it will imidiarely close the server

2 comments

0

also i forgot to say it may corrupt the data when it save them if the server shut down faster than it should

0

and also remember not many people read scripts with more than 30 lines

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