Datasave not working after moving values

Hello! I need some help, i am trying to achieve data saving, but for some reason this error keeps showing up:

The datasave worked before, but now it doesnt. All i did was move a few more stats into other folders when creating:
image

Can anyone please help???

Here is the datasave code:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
	local Data = nil
	local success, errormessage = pcall(function()
		Data = Saver:GetAsync(tostring(player.UserId))
	end)
	if success then
		if Data then
			for i, v in pairs(Data) do
				player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
			end
			for i, v in pairs(Data) do
				player:WaitForChild("Stats"):WaitForChild(i).Value = v
			end
			for i, v in pairs(Data) do
				player:WaitForChild("Ores"):WaitForChild(i).Value = v
			end
			for i, v in pairs(Data) do
				player:WaitForChild("Multipliers"):WaitForChild(i).Value = v
			end
		end
	else
		error(errormessage)
	end
end)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		SavedData[v.Name] = v.Value
	end
	
	for _, v in pairs(player.Stats:GetChildren()) do
		SavedData[v.Name] = v.Value
	end
	
	for _, v in pairs(player.Ores:GetChildren()) do
		SavedData[v.Name] = v.Value
	end
	
	for _, v in pairs(player.Multipliers:GetChildren()) do
		SavedData[v.Name] = v.Value
	end

	local success, errormessage = pcall(function()
		Saver:SetAsync(tostring(player.UserId), SavedData)
	end)
	if not success then
		error(errormessage)
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)

You are trying to find “MeleeStat” as a child of leaderstats.

2 Likes

Exactly, he needs to change how he calls things like leaderstats. Also I don’t recommend just making folders with attachments to the player and number values, this is an older way of doing data saving and it can easily be exploited.

Thanks for the help everyone! I figured it out after some time. Appreciate it! Cheers. :grin:

I have taken a break from scripting for a month or two now but how exactly can it be exploited? I don’t think they’d be able to change anything because they’re a client so it won’t make a change on the server. They’d just be messing themselves up.

And why is it not recommended? How else can you make leaderstats for a player without this method? Would you have to make a custom leaderstats bar?

I didn’t say leaderstats alone, I think you should look into profileservice and other data saving methods. Folders attached to a character is more a visual thing. The data itself should be kept private where you can’t see it. Leaderstats can be attached to a player with out them seeing it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.