Leaderstats String Value Help

Hello DevForum, I kinda need help on leaderstats. My problem is that I made a stat and I set it as a string value, then I set the value as ‘Mizunoto’. But when I test it out, it just says the value is set as 0. Can anybody help me please?

Here’s my code:

local players = game:GetService("Players")

players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'
	
	local Yen = Instance.new("NumberValue", player)
	Yen.Name = "Yen"
	Yen.Value = 0
	
	local Rank = Instance.new("StringValue", leaderstats)
	Rank.Name = "Rank"
	Rank.Value = "Mizunoto"
end)

I also do have a data store script, but I’m pretty sure it doesn’t affect the leaderstats script. But here is the script anyways,

local Players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saver = dataStoreService:GetDataStore("saveLeaderstats")

Players.PlayerAdded:connect(function(player)
	local Data = nil
	local succes, errorMessage = pcall(function()
		Data = saver:GetAsync(tostring(player.UserId))
	end)
	if succes then
		if Data then
			for i, v in pairs(Data) do
				player:WaitForChild("leaderstats"):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
	local succes, errorMessage = pcall(function()
		saver:SetAsync(tostring(player.UserId), savedData)
	end)
	if not succes then
		error(errorMessage)
	end
end

Players.PlayerRemoving:Connect(Save)

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

Also sorry if this post seems weird, this is my first time posting.

1 Like

Your leaderstats script works for me. One thing though, you shouldn’t use the second parameter of Instance.new but instead assign the parent after.

Code without the second parameter:

local players = game:GetService("Players")

players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player
	
	local Yen = Instance.new("NumberValue")
	Yen.Name = "Yen"
	Yen.Value = 0
    Yen.Parent = player
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Value = "Mizunoto"
    Rank.Parent = leaderstats
end)