Why Is it not saving leaderstats

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

saving data whenever the player leaves, or bindtoclose

  1. What is the issue? Include screenshots / videos if possible!

iam trying to make it save the data, but its does not work , iam getting no errors

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes i follow totutiols on youtube

local Data = DataStore:GetDataStore("Data")

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

	local Power = Instance.new("NumberValue")
	Power.Name = "Power"
	Power.Parent = leaderstats
	Power.Value = 15

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = leaderstats
	Rebirths.Value = 0

	local MaxLine = Instance.new("NumberValue")
	MaxLine.Parent = player
	MaxLine.Name = "MaxLine"
	MaxLine.Value = 100

	local MutiPiler = Instance.new("NumberValue")
	MutiPiler.Parent = player
	MutiPiler.Name = "MutiPiler"
	MutiPiler.Value = 1

	local Speed = Instance.new("NumberValue")
	Speed.Parent = player
	Speed.Name = "Speed"
	Speed.Value = 1
	
	local NewData
	
	local success, errormessage = pcall(function()
		NewData = Data:SetAsync(player.UserId.. "Power", player.leaderstats.Power.Value)
	end)
	
	if success then
		print("Success")
		Power.Value = NewData
	else
		print("ERROR")
		warn(errormessage)
	end
end


function PlayerRemoving(player)
	local success, errormessage = pcall(function()
		Data:SetAsync(player.UserId.. "Power", player.leaderstats.Power.Value)
	end)
	
	if success then
		print("success fully Saved")
	else
		print("Error")
		warn(errormessage)
	end
end

function BindToClose()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local success, errormessage = pcall(function()
			Data:SetAsync(player.UserId.. "Power", player.leaderstats.Power.Value)
		end)

		if success then
			print("success fully Saved")
		else
			print("Error")
			warn(errormessage)
		end
	end
end

game.Players.PlayerAdded:Connect(playerJoined)
game.Players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(BindToClose)
2 Likes

In your playeradded function you’re using SetAsync. Replace it with GetAsync or UpdateAsync (a little better).

yes it works, but whenever i join the first ever time my power value is 0 how can i set it to 15?

Just check if the player has data. If they don’t have data, just add 15 to their stats. If they have data, just load it.

If they don’t have data that probably means it’s their first time playing

thank you its works, but how would i save muiltple Values, With out haveing too much Code

I mean I don’t really have a solution to that because I usually never save a lot of data in my games. Sorry :grimacing:

i figured it out , but thanks for helping me

local DataStore = game:GetService("DataStoreService")

local Data = DataStore:GetDataStore("Data")

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

	local Power = Instance.new("NumberValue")
	Power.Name = "Power"
	Power.Parent = leaderstats

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = leaderstats

	local MaxLine = Instance.new("NumberValue")
	MaxLine.Parent = player
	MaxLine.Name = "MaxLine"
	MaxLine.Value = 100

	local MutiPiler = Instance.new("NumberValue")
	MutiPiler.Parent = player
	MutiPiler.Name = "MutiPiler"
	MutiPiler.Value = 1

	local Speed = Instance.new("NumberValue")
	Speed.Parent = player
	Speed.Name = "Speed"
	Speed.Value = 1
	
	local NewData
	
	local data = Data:GetAsync(player.UserId)
	
	if data then
		Power.Value = data['Power']
		Rebirths.Value = data['Rebirths']
	else
		Power.Value = 15
		Rebirths.Value = 0
	end
end

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

> Blockquote

return Player_Stats
end


function PlayerRemoving(player)
	local Player_Stats = CreateTable(player)
	
	local success, errormessage = pcall(function()
		Data:SetAsync(player.UserId, Player_Stats)
	end)
	
	if success then
		print("success fully Saved")
	else
		print("Error")
		warn(errormessage)
	end
end

function BindToClose()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local Player_Stats = CreateTable(player)

		local success, errormessage = pcall(function()
			Data:SetAsync(player.UserId, Player_Stats)
		end)

		if success then
			print("success fully Saved")
		else
			print("Error")
			warn(errormessage)
		end
	end
end

game.Players.PlayerAdded:Connect(playerJoined)
game.Players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(BindToClose)

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