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

DataStore not working when I change the name of DataStore?

This is my script for creating and saving the 'Skips' value inside the player:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SkipsDataStore")

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

	local Skips = Instance.new("IntValue", SkipStages)
	Skips.Name = "Skips"
	Skips.Value = 3


	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)

	if success then
		Skips.Value = data.Skips
	else
		print("Error while fetching data!")
		warn(errormessage)
	end


end)


game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		local data = {
			Skips = player.SkipStages.Skips.Value
		}

		DataStore:SetAsync(player.UserId, data)
	end)

	if success then
		print("Skips Successfully Saved!")
	else
		print("There was an error while saving Skips!")
		warn(errormessage)
	end

end)

Currently, I have named my DataStore as "SkipsDataStore". If I change that name to something else, then my data does not save and the output gives me the error, "* ServerScriptService.Skips:19: attempt to index nil with 'Skips'*"

But the above error is ONLY when I change the name to something other than "SkipsDataStore".

Why is that? I have been using DataStores before also and I was able to change their name without any errors.

So why is this happening?

Thanks for any help

2 answers

✓ Accepted answer

The reason for this, I suppose, is that there is no saved data. So, the error is trying to say by attempt to index nil by Skips, here, the 'Skips' is not Skips.Value, but the error is indexing data.Skips. In simple words, the error is trying to say that data is nil.

To fix your issue, you can simple do on Line 18 :

if success and data then -- It will ensure whether the player has saved data

Lemme know if it helps!

EDIT : I also noticed that there is no BindToClose function. Here is an example (make sure to add it at the end of your code) :

local RunS = game:GetService("RunService")

game:BindToClose(function()
	if RunS:IsStudio() then return end -- I will explain at the end why I added this

	-- Starting a loop to save every player's data
	for _, plr in ipairs(game.Players:GetPlayers()) do
		-- Your code of saving data
	end
end) 

Explanation : BindToClose is a function that helps to delay the server shutdown when the last player leaves or when the game is being shut down. It will ensure every player's data is saved.

if RunS:IsStudio() then return end prevents BindToClose to fire in Studio because when you test in Studio and you end the end, BindToClose triggers and delays the test shutdown. Due to which, PlayerRemoving also fires and it may lead to queue requests in DataStores, which results in no data saving.

Information about BindToClose in Roblox Dev:

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

2 comments

0

Thank you! Just one question regarding bindtoclose function. If I am implementing this in my code, then should I remove the player removing function? doesn't bindtoclose function take care of that? If so, then where you wrote ' you code of saving data' should I add what I wrote in player removing event?

0

DO NOT REMOVE PLAYER REMOVING. As BindToClose will only trigger when the server is about to close. And ya, where I wrote 'your code of saving data', you can add the code in PlayerRemoving. If you using identical variables as my code, change the plr variabe in BindToClose to player.

You can do what BestCreativeBoy's answer said:

if success and data then
	Skips.Value = data.Skips
else
	print("Error while fetching data!")
	warn(errormessage)
end

in the success part. Hope this helps!

2 comments

0

Well, this thing is un-neccessary. The reason why didn't I add default data is because @Soban06 is setting the value of Skips to 3. So, if no data is found, it means that a new player has joined, and value will remain to what @Soban06 has set.

0

ok

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