Why doesn't this datastore work?

local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local data = Datastore:GetDataStore("data") 

game.Players.PlayerAdded:Connect(function(player)
	--creating values
	local leadstats = Instance.new("Folder")
	leadstats.Name = "leaderstats"
	local checkpoints1 = Instance.new("IntValue", leadstats)
	checkpoints1.Name = "Checkpointch1"
	local temp = Instance.new("IntValue", leadstats)
	temp.Name = "temp"
	local chapters = Instance.new("IntValue", leadstats)
	chapters.Name = "chapters"

	--values
	local playerUserId = "Player_"..player.UserId
	leadstats.Parent = player

	local success,save = pcall(function()
		return  data:GetAsync(playerUserId)
	end)

	if success then
		-- Setting saved data to player 
		checkpoints1.Value = save['checkpoints1'] or 1
		chapters.Value = save['chapters'] or 1 
		print("Data info:", "checkpoints:", checkpoints1.Value ..",")
		print("chapters:", chapters.Value ..",")
		print("temp", temp.Value)
		script:SetAttribute("Loaded", true)

	else
		repeat checkpoints1.Value = save['checkpoints1'] or 1
			chapters.Value = save['chapters'] or 1 
			print("Data info:", "checkpoints:", checkpoints1.Value ..",")
			print("chapters:", chapters.Value ..",")
			print("temp", temp.Value)
			script:SetAttribute("Loaded", true) until success
		local webhookdata = {["embeds"] = {{
			["title"] = "Error",
			["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
		local webhookmsg = https:JSONEncode(webhookdata)
		task.wait(30)
		https:PostAsync(url, webhookmsg)
		warn(save)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	--saving
	local playerUserId = "Player_"..player.UserId
	-- Player int values
	local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
	local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
	local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")

	local success, errormsg
	success, errormsg = pcall(function()
		local save
		--table
		if script:GetAttribute("Loaded" == true) then
			local savedata = {}
			savedata['chapters'] = chapters.Value
			savedata['checkpoints1'] = checkpoints1.Value
			print(savedata)
			save = data:SetAsync(playerUserId, savedata)
		end
	end)
	if success then
		print("Data saved")
	else
		local webhookdata = {
			["embeds"] = {
				{
					["title"] = "Error",
					["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
			}
		}
		local webhookmsg = https:JSONEncode(webhookdata)

		https:PostAsync(url, webhookmsg)
		warn(errormsg)
	end
end)

it either doesn’t save or load.
i tried using bindtoclose but it didn’t work.

What exactly is the problem? All you’ve given us is the block of code so we have nothing to work with. We don’t understand what the bug is. Could you explain a bit more?

it either doesn’t save or load. Sorry for not telling the problem!

Ah I see the usual problem, I’ll quickly load this into my baseplate and see the problem.

also here is more info: the game is singleplayer

Line 1: local datastore2 = require(script.Datastore2)

Does this exist because you’re not using it inside the script? It’s also line 1 meaning if it doesn’t exist you break the entire script.

1 Like

yes, it is inside of the script. It’s datastore2 to ensure no dataloss (i just suck at datastores so i need it lol)

The script overall seems normal but I adjusted it slightly and removed some lines that were unnecessary.


local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local datastore = Datastore:GetDataStore("data")
local data

game.Players.PlayerAdded:Connect(function(player)
	--creating values
	local leadstats = Instance.new("Folder")
	leadstats.Name = "leaderstats"
	local checkpoints1 = Instance.new("IntValue", leadstats)
	checkpoints1.Name = "Checkpointch1"
	local temp = Instance.new("IntValue", leadstats)
	temp.Name = "temp"
	local chapters = Instance.new("IntValue", leadstats)
	chapters.Name = "chapters"

	--values
	local playerUserId = "Player_"..player.UserId
	leadstats.Parent = player

	local success,save = pcall(function()
		data = datastore:GetAsync(playerUserId)
	end)

	if success then
		-- Setting saved data to player 
		checkpoints1.Value = save['checkpoints1'] or 1
		chapters.Value = save['chapters'] or 1 
		print("Data info:", "checkpoints:", checkpoints1.Value ..",")
		print("chapters:", chapters.Value ..",")
		print("temp", temp.Value)
		script:SetAttribute("Loaded", true)

	else
		repeat checkpoints1.Value = save['checkpoints1'] or 1
			chapters.Value = save['chapters'] or 1 
			print("Data info:", "checkpoints:", checkpoints1.Value ..",")
			print("chapters:", chapters.Value ..",")
			print("temp", temp.Value)
			script:SetAttribute("Loaded", true) until success
		local webhookdata = {["embeds"] = {{
			["title"] = "Error",
			["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
		local webhookmsg = https:JSONEncode(webhookdata)
		task.wait(30)
		https:PostAsync(url, webhookmsg)
		warn(save)
	end
end)

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

	--saving
	local playerUserId = "Player_"..player.UserId
	-- Player int values
	local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
	local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
	local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")

	local success, errormsg = pcall(function()
		local save
		--table
		if script:GetAttribute("Loaded" == true) then
			local savedata = {}
			savedata['chapters'] = chapters.Value
			savedata['checkpoints1'] = checkpoints1.Value
			print(savedata)
			save = data:SetAsync(playerUserId, savedata)
		end
	end)
	if success then
		print("Data saved")
	else
		local webhookdata = {
			["embeds"] = {
				{
					["title"] = "Error",
					["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
			}
		}
		local webhookmsg = https:JSONEncode(webhookdata)

		https:PostAsync(url, webhookmsg)
		warn(errormsg)
	end
end)

Edit: Forgot to add webhook back

question: how could i add a bindtoclose to this? i know it’s not really related to the original question but i am really struggling on datastore.

Full script with BindToClose added:


local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local datastore = Datastore:GetDataStore("data")
local data

game.Players.PlayerAdded:Connect(function(player)
	--creating values
	local leadstats = Instance.new("Folder")
	leadstats.Name = "leaderstats"
	local checkpoints1 = Instance.new("IntValue", leadstats)
	checkpoints1.Name = "Checkpointch1"
	local temp = Instance.new("IntValue", leadstats)
	temp.Name = "temp"
	local chapters = Instance.new("IntValue", leadstats)
	chapters.Name = "chapters"

	--values
	local playerUserId = "Player_"..player.UserId
	leadstats.Parent = player

	local success,save = pcall(function()
		data = datastore:GetAsync(playerUserId)
	end)

	if success then
		-- Setting saved data to player 
		checkpoints1.Value = save['checkpoints1'] or 1
		chapters.Value = save['chapters'] or 1 
		print("Data info:", "checkpoints:", checkpoints1.Value ..",")
		print("chapters:", chapters.Value ..",")
		print("temp", temp.Value)
		script:SetAttribute("Loaded", true)

	else
		repeat checkpoints1.Value = save['checkpoints1'] or 1
			chapters.Value = save['chapters'] or 1 
			print("Data info:", "checkpoints:", checkpoints1.Value ..",")
			print("chapters:", chapters.Value ..",")
			print("temp", temp.Value)
			script:SetAttribute("Loaded", true) until success
		local webhookdata = {["embeds"] = {{
			["title"] = "Error",
			["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
		local webhookmsg = https:JSONEncode(webhookdata)
		task.wait(30)
		https:PostAsync(url, webhookmsg)
		warn(save)
	end
end)

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

	--saving
	local playerUserId = "Player_"..player.UserId
	-- Player int values
	local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
	local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
	local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")

	local success, errormsg = pcall(function()
		local save
		--table
		if script:GetAttribute("Loaded" == true) then
			local savedata = {}
			savedata['chapters'] = chapters.Value
			savedata['checkpoints1'] = checkpoints1.Value
			print(savedata)
			save = data:SetAsync(playerUserId, savedata)
		end
	end)
	if success then
		print("Data saved")
	else
		local webhookdata = {
			["embeds"] = {
				{
					["title"] = "Error",
					["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
			}
		}
		local webhookmsg = https:JSONEncode(webhookdata)

		https:PostAsync(url, webhookmsg)
		warn(errormsg)
	end
end)

game:BindToClose(function()
	wait(2)
end)

Simple bind to close:


game:BindToClose(function()
	wait(2)
end)
1 Like

Did the script work? If it did I can tell you what I changed and if it didn’t I can look further into it.

If you’re using the webhook for discord I just want to remind you that discord blocked webhook request from Roblox.

does not work. also i am using hyra to bypass the request block. i found the problem. “Attempted to index nil with ‘Checkpointsch1’”

Oh nice, you found the problem do you already have a way to fix it?

i don’t know. i think the issue may be that the value isn’t created yet

Try using:
local datastore2 = require(script:WaitForChild(“Datastore2”))
sometimes the script may have just not loaded in

I have no idea, maybe you should debug with print() and see what values are nil at the start and end.

done but i’ve changed my code to @DonKingFrog’s code and i keep getting: ServerScriptService.SavingScript:30: attempt to index nil with ‘checkpoints1’

Then maybe checkpoints1 isn’t created

I understand now the problem: You haven’t saved anything right? you have to change your code from if success then to if data then:


local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local datastore = Datastore:GetDataStore("data")
local data

game.Players.PlayerAdded:Connect(function(player)
	--creating values
	local leadstats = Instance.new("Folder")
	leadstats.Name = "leaderstats"
	local checkpoints1 = Instance.new("IntValue", leadstats)
	checkpoints1.Name = "Checkpointch1"
	local temp = Instance.new("IntValue", leadstats)
	temp.Name = "temp"
	local chapters = Instance.new("IntValue", leadstats)
	chapters.Name = "chapters"

	--values
	local playerUserId = "Player_"..player.UserId
	leadstats.Parent = player

	local success,save = pcall(function()
		data = datastore:GetAsync(playerUserId)
	end)

	if data then
		-- Setting saved data to player 
		checkpoints1.Value = save['checkpoints1'] or 1
		chapters.Value = save['chapters'] or 1 
		print("Data info:", "checkpoints:", checkpoints1.Value ..",")
		print("chapters:", chapters.Value ..",")
		print("temp", temp.Value)
		script:SetAttribute("Loaded", true)

	else
		repeat checkpoints1.Value = save['checkpoints1'] or 1
			chapters.Value = save['chapters'] or 1 
			print("Data info:", "checkpoints:", checkpoints1.Value ..",")
			print("chapters:", chapters.Value ..",")
			print("temp", temp.Value)
			script:SetAttribute("Loaded", true) until success
		local webhookdata = {["embeds"] = {{
			["title"] = "Error",
			["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
		local webhookmsg = https:JSONEncode(webhookdata)
		task.wait(30)
		https:PostAsync(url, webhookmsg)
		warn(save)
	end
end)

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

	--saving
	local playerUserId = "Player_"..player.UserId
	-- Player int values
	local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
	local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
	local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")

	local success, errormsg = pcall(function()
		local save
		--table
		if script:GetAttribute("Loaded" == true) then
			local savedata = {}
			savedata['chapters'] = chapters.Value
			savedata['checkpoints1'] = checkpoints1.Value
			print(savedata)
			save = data:SetAsync(playerUserId, savedata)
		end
	end)
	if success then
		print("Data saved")
	else
		local webhookdata = {
			["embeds"] = {
				{
					["title"] = "Error",
					["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
			}
		}
		local webhookmsg = https:JSONEncode(webhookdata)

		https:PostAsync(url, webhookmsg)
		warn(errormsg)
	end
end)

game:BindToClose(function()
	wait(2)
end)