What is wrong with my datastore?

code:

local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = "webhook url"
local data = Datastore:GetDataStore("data") 
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"
--loading
game.Players.PlayerAdded:Connect(function(player)
	--values
	playerUserId = "Player_"..player.UserId
	leadstats.Parent = player
	--loading
	local save
	local success, errormsg = pcall(function()
		save = data:GetAsync(playerUserId)
	end)
	success = false
	if success then
		checkpoints1.Value = save
		chapters.Value = save
	elseif checkpoints1.Value == nil then
		checkpoints1.Value = 0
	elseif chapters.Value == nil then
	chapters.Value = 0
	
	else
		--pcall
		repeat save = data:GetAsync(playerUserId, chapters.Value, checkpoints1.Value) 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)
		https:PostAsync(url, webhookmsg)
		warn(errormsg)
	end
end)
--saving

game.Players.PlayerRemoving:Connect(function(player)
	--saving
	local playerUserId = "Player_"..playerUserId
	local success, errormsg
	success, errormsg = pcall(function()
		local save
		save = data:UpdateAsync(playerUserId, chapters.Value, checkpoints1.Value)
			end)
		if success then
			print("Data saved")
		else
			--pcall
			warn(errormsg)
			local webhookdata = {["embeds"] = {{
				["title"] = "Error",
				["description"] = player.Name, "Something went wrong while saving data."}}}
			local webhookmsg = https:JSONEncode(webhookdata)
			https:PostAsync(url, webhookmsg)
		end
	end)
end)

i changed the value via console in roblox, not in studio. i have no idea which doesn’t work. also not i am not getting a message with the webhook nor are there any errors.

I don’t know why you set success to false; in the very next line, your code checks if it’s true and loads the player save data

This check isn’t written correctly; You set checkpoints1 and chapters to the same value, even though it seems like you’re trying to save two separate values later in your script (In addition, your first check will always be false because of the previous line)

Your PlayerAdded connection should be modeled after the following:

local success, output = pcall(function()
   return data:GetAsync(playerUserId) -- Returns the save data to the 'output' variable
end)

if success then
   if output then
       -- This checks if the call was both successful and if the player has data to load
       local checkpoint1, chapters = output[1], output[2] -- Since you should be storing a table, you need to get the proper values by indexing them
       -- Load save data
   else
      -- Data doesn't exist
   end
else
   -- Call wasn't successful
end

This isn’t how you use UpdateAsync; the first value is the key and second value is the transform function that returns the new value of the datastore

local success, message = pcall(function()
    data:UpdateAsync(playerUserId, function(oldValue)
         -- 'oldValue' is the old data stored with the key
         return {} -- This should a table that holds the new values that should be saved
    end)
end)

if success then
    -- Update was successful
else
    -- Update wasn't successful
end

P.S. you shouldn’t be using webhooks for logging data storage


https://create.roblox.com/docs/scripting/data/data-stores

i tried testing if something worked before but i removed the thing and forgot to remove the boolean. would you mind explaining me how to continue trying saving in case it didn’t work the first time? i know i’ll have to use bindtoclose.

– ‘oldValue’ is the old data stored with the key
return {} – This should a table that holds the new values that should be saved

could you explain this? i don’t really understand return

Yo, I fixed your code. (My English is bad sorry for that)
I have some issues with what I found in your code

So first, your code do not create data folder for each player who joined the game.
I just moved lines to PlayerAdded Function
I don’t know why you set value success to false, I removed this useless thing

Okay for the saving function I think you cant save multiples value in one async so you must create a before array with these values and set indexes for a single one
then you can easily save without wasting time

The code is working. I tried and work
But I want you to understand why your code isn’t working and my works
Code:

local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = "webhook url"
local data = Datastore:GetDataStore("data") 

game.Players.PlayerAdded:Connect(function(player)
	-- CREATING FOR EVERY SINGLE PLAYER FOLDER DATA
	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)
	print("-----PLAYER SAVED DATA-----")
	print(save)
	print("----------")
	if success then
		-- Setting saved data to player 
		checkpoints1.Value = save['checkpoints1'] or 1
		chapters.Value = save['chapters'] or 1 
	else
		error(">>> Something goes wrong in loading data...")
	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
		--Creating array for saving single table (The best solution i think)
		local savedata = {}
		savedata['chapters'] = chapters.Value
		savedata['checkpoints1'] = checkpoints1.Value
		savedata['temp'] = temp.Value
		print(savedata)
		save = data:SetAsync(playerUserId, savedata)
	end)
	if success then
		print(">>> Data is correctly saved")
	else
		warn(errormsg)

	end
end)
1 Like