Misterious new value appearing out of nowhere when loading datastore into the leaderstats

I’m having this issue where a fourth value shows up on the leaderstats for some players, sometimes it’s me sometimes it’s my friend, and I have no Idea what’s causing it
(I’m not using any local script to change the leaderstats)


here’s my messy code if it is of any use
this is my first time using datastores so all of this is confusing to me

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"

	local horesecoins = Instance.new("IntValue", leaderstats)
	
	local wins = Instance.new("IntValue", leaderstats)
	
	local everykill = Instance.new("IntValue", leaderstats)
	
	local kills = Instance.new("IntValue", plr)
	
	local success, data = pcall(function()
		return statStore:GetAsync(tostring(plr.UserId))
	end)
	
	if success then
		if not data then
			data = {0, 0, 0}
		end
		everykill.Value = data[1]
		horesecoins.Value = data[2]
		wins.Value = data[3]
	else
		warn("fail")
	end
	
	wins.Name = "wins"
	
	horesecoins.Name = "horse coins"

	everykill.Name = "all kills"
	
	kills.Name = "kills"
end)


game.Players.PlayerRemoving:Connect(function(plr)
	local allkillsval = plr.leaderstats:WaitForChild("all kills").Value
	local horsecoinsval = plr.leaderstats:WaitForChild("horse coins").Value
	local winsval = plr.leaderstats:WaitForChild("wins").Value

	
	
	local success, err = pcall(function()
		statStore:SetAsync(tostring(plr.UserId),{allkillsval, horsecoinsval, winsval})
	end)
end)

Please try to remove “kills” from leaderstats(it is okay to add – before the passage)

but why? the kills intvalue is not displayed in the leaderstats, it’s just there for convinience, it doesn’t even save

1 Like

are you sure that inside each Player Instance there is only one leaderstats folder? cause once, every time I started my game, two folders called “leaderstats” would conflict. maybe you didn’t notice that another script could have created another leaderstats folder

1 Like

I believe he is trying to see if the random value is related to the kill value. Which I believe it is based on your two screenshots, as in the top one is value = 38 and on the bottom one its all kills = 38


I would try implementing this function to see if you have a value that gets added in somewhere. Like do you have another script that makes adjustments to these stats?

local function printLeaderstats(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then
		warn(player.Name .. " has no leaderstats")
		return
	end

	for _, stat in ipairs(leaderstats:GetChildren()) do
		if stat:IsA("ValueBase") then
			print(stat.Name .. ":", stat.Value)
		end
	end
end

You can try calling it when the player joins and leaves.


If the random value is related to the kill value. I would try saving the kill value somewhere else instead of inside the player as it is probably getting added to leader stats anyways.

The leaderstats Gui only shows the instances inside the folder, not even all other values ​​outside of leaderstats


1 Like

Yeah, i checked and each player only has one leaderstats folder

I was misunderstanding that kills is a child of leaderstats.

Please insert print(data) after GetAsync. Also, please add

 game.IsLoaded:Connect(function()
      RemoteEvent.FireServer()
    end
)

on LocalScript and use

RemoteEvent.OnServerEvent:Connect()

Instead of PlayerAdded:Connect()

This is just a try, not solution.

That code, alone, has no way of creating an extra Value object inside the leaderstats folder.
Please check if you have any other scripts in your game (since you said it only happens for some players and not others, likely a LocalScript) that is creating an extra instance inside the leaderstats folder that maybe you forgot to remove.

In Studio, press Ctrl + Shift + F to open the search tool for all scripts in your game. You can type in “leaderstats” inside of it to see all scripts that reference it in your code; this will help you find it.