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

The function to save my data doesn't work, help?

This piece of code from a big block seems to be the problem. It doesn't print out "Save 2"

local function SaveData(player)
	if player.userId < 0 then return end
	player:WaitForChild("leaderstats")
	wait()
	local leaderstats = {}
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		if not stat:IsA("Folder") then
			print("Save 1")
			table.insert(leaderstats, {stat.Name, stat.Value})
		end
	end
	player:WaitForChild("leaderstats"):WaitForChild("Rebirth")
	wait()
	for i, stat in pairs(player.leaderstats.Rebirths:GetChildren()) do
		print("Save 2")
		table.insert(leaderstats, {stat.Name, stat.Value})
	end
	leaderboardData:SetAsync(player.userId, leaderstats)
	Print("Saved "..player.Name.."'s data")
end

leaderstats folder: https://drive.google.com/file/d/1j1v2ZnDaCvP1ifK-H1XgtWIJffGFppzW/view?usp=sharing

8 comments

0

I can't see the file. 'Access denied'

0

oh

0

it should work now

0

Is there any errors? it seems that it should work.

0

no errors

0

but it doesnt work

0

did you run the function? can you give the script where you run it?

0

oh, and enable studio acces to api service in game settings. Did you enable it yet?

2 answers

I just got confused with the value "Rebirths" and the folder "Rebirth" sry everybody

1 comment

0

i see, how did i not notice that, lol.

in for i,v (save2) you have leaderstats.Rebirths but before for i,v you have WaitForChild("Rebirth") and not Rebirths

or

use this script and put all in 1 folder

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game:GetService("Players").PlayerAdded:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local data = playerData:GetAsync(playerUserId)

	local Folder = Instance.new("Folder", player)
	Folder.Name = "leaderstats"

	local XP = Instance.new("NumberValue", Folder)
	XP.Name = "XP" -- this is example

	local LVL = Instance.new("NumberValue", Folder)
	LVL.Name = "Levels"

	local XPN = Instance.new("BoolValue", Folder)
	XPN.Name = "XPN"

	if data then
		XP.Value = data["XP"]
		LVL.Value = data["Levels"]
		XPN.Value = data["XPN"]
	else
		XP.Value = 0
		LVL.Value = 0
		XPN.Value = true
	end
end)

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = "Player_"..player.UserId
		playerData:SetAsync(playerUserId, player_stats)
	end)
	if not success then
		warn("Could not save data")
	end

end)

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