Data Store problem

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Rolls")

players.PlayerAdded:Connect(function(plr)
	local character = plr.CharacterAdded:Wait()
	local CAValue : NumberValue = character:WaitForChild("CurrentAura")
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	local Rolls = Instance.new("IntValue", leaderstats)
	Rolls.Name = "Rolls"

	local success, metadatas = pcall(function()
		return datastore:GetAsync(plr.UserId) 
	end)

	if metadatas then
		Rolls.Value = metadatas.Rolls
		if metadatas.CurrentStor then
			CAValue.Value = metadatas.CurrentStor
		else
			CAValue.Value = 1
		end
		
		for i, data in pairs(plr.Character:WaitForChild("StorageOfAuras"):GetDescendants()) do
			if metadatas.Stroage then
				data.Value = metadatas.Storage
			else
				data.Value = "None"
			end
		end
	else
		Rolls.Value = 0
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local value = plr.Character:FindFirstChild("StorageOfAuras")
	local metadatas = nil
	for i, data in pairs(value:GetDescendants()) do
		local stores = tonumber(data.Name)
		if stores then
			metadatas = {["Rolls"] = plr:WaitForChild("leaderstats").Rolls.Value, ["Storage"] = data.Value, ["CurrentStor"] = plr.Character.CurrentAura.Value}
		end
	end
	local success, errormsg = pcall(function()
		return datastore:SetAsync(plr.UserId, metadatas)
	end)

	if success then
		print("cool bro your data saved so ezz")
	else
		error(errormsg)
	end
end)

data doesnā€™t saving/load.

thereā€™s a typo in your code that might be causing the problem. you wrote ā€œStroageā€ instead of ā€œStorageā€ when checking if the metadata exists. hereā€™s the corrected portion:

for i, data in pairs(plr.Character:WaitForChild("StorageOfAuras"):GetDescendants()) do
	if metadatas.Storage then
		data.Value = metadatas.Storage
	else
		data.Value = "None"
	end
end

also, when saving data, youā€™re creating a new table every time you find a descendant of ā€œStorageOfAurasā€, which is not ideal. you should create the table once and then update it. hereā€™s how you can fix it:

players.PlayerRemoving:Connect(function(plr)
	local value = plr.Character:FindFirstChild("StorageOfAuras")
	local metadatas = {["Rolls"] = plr.leaderstats.Rolls.Value, ["CurrentStor"] = plr.Character.CurrentAura.Value}
	
	for i, data in pairs(value:GetDescendants()) do
		if tonumber(data.Name) then
			metadatas["Storage"] = data.Value
		end
	end
	
	local success, errormsg = pcall(function()
		return datastore:SetAsync(plr.UserId, metadatas)
	end)

	if success then
		print("cool bro your data saved so ezz")
	else
		error(errormsg)
	end
end)

if thereā€™s any other issue, donā€™t hesitate to ask

1 Like

idk but this is doesnt work. heres other script:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local character = player.Character
		local auravalue = Instance.new("NumberValue")
		auravalue.Parent = character
		auravalue.Name = "CurrentAura"
		auravalue.Value = 1
		local rollscount = Instance.new("IntValue")
		rollscount.Parent = character
		rollscount.Name = "RollsCounter"
		rollscount.Value = 0
		local folder = Instance.new("Folder")
		folder.Parent = character
		folder.Name = "StorageOfAuras"
		for i, stor in pairs(player.PlayerGui:WaitForChild("Main").invertory.AurasList:GetChildren()) do
			local number = tonumber(stor.Name)
			if number then
				local valuestor = Instance.new("StringValue")
				valuestor.Name = number
				valuestor.Parent = folder
				valuestor.Value = "None"
			end
		end
		character:SetAttribute("QK", false)
		local surface = script.GUIPlayers:Clone()
		surface.Parent = character:WaitForChild("HumanoidRootPart")
		local antidebounce = false

		rollscount.Changed:Connect(function()
			if antidebounce == false then
				player.leaderstats.Rolls.Value = player.leaderstats.Rolls.Value + 1
				antidebounce = true
				wait(0.3)
				antidebounce = false
			end
		end)
	end)
end)

This script is basically not related to that script, but it can help

1 Like

try adding more debug print messages, and make sure that the data youā€™re trying to save is correctly structured and that all keys and values are valid

Sometimes its print: ServerScriptService.CharacterAdded.Leaderboard:40: attempt to index nil with ā€˜CurrentAuraā€™

on this line:
local metadatas = {["Rolls"] = plr.leaderstats.Rolls.Value, ["CurrentStor"] = plr.Character.CurrentAura.Value}

Ohh, alright

you should use WaitForChild() so that you make sure the currentaura is found

try this script

players.PlayerRemoving:Connect(function(plr)
    local character = plr.Character or plr.CharacterAdded:Wait()
    local currentAura = character:WaitForChild("CurrentAura", 10) -- // wait up to 10 seconds for CurrentAura to exist \\ --

    if not currentAura then
        warn("CurrentAura not found for player " .. plr.UserId)
        return
    end

    local metadatas = {
        ["Rolls"] = plr.leaderstats.Rolls.Value,
        ["CurrentStor"] = currentAura.Value
    }
    -- // the rest of your saving script... \\ --
end)

tell me if thereā€™s any other issue

make sure that you also save if the game is binding to close

as said here (How should I be using BindToClose()? - #42 by P1X3L_K1NG)

I have plr variables and they are used in the code for saving

noā€¦ you still need to account for bind to close, trust me, i have had this issue before and thats how i fixed it.

this code is doesnt work still

still doesnā€™t work bruh. they doesnā€™t saving anything