2 Different Leaderstats

Hello! Been a while and I am finally starting to get back into coding, I am making a pretty simple game currently and am trying to have 2 leaderstat scripts, one being coins that shows on the player list in game, and the other being health, that doesnt show. I’ve tried adding a few things but each time it fails and either the coins will show or the health will show.
Anyone have some solutions?

Heres the code im using for the health leaderstats, the coins one is the same thing just stored in “moneystats” instead of “healthstats”. When I test the game, only one of them show up, but if i rejoin the other leaderstats will show.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("HealthStats")



game.Players.PlayerAdded:Connect(function(Player)
	local healthstats = Instance.new("Folder")
	healthstats.Name = "leaderstats"
	healthstats.Parent = Player
	local Health= Instance.new("IntValue")
	Health.Name = "Health"
	Health.Value = 100
	Health.Parent = healthstats
		
	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Health.Value = Data.Health


	else
		print("There Was An Error Saving"..Player.UserId " 's Health Data :(")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Health"] = Player.leaderstats.Health.Value;

	})


end)

Thanks! I know its probably simple as every other silly post I’ve made but I couldnt find the solution by myself :wink:

(just again… goal is to have coins leaderstats is visible while the health isnt.)

I would recommend creating the leaderstats in one script. You should only have one leaderstats folder, with many values inside of it.

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
-- add health to leaderstats
	local Health= Instance.new("IntValue")
	Health.Name = "Health"
	Health.Value = 100
	Health.Parent = leaderstats
-- now add money to the same leaderstats
	local money= Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 0
	money.Parent = leaderstats

I agree, in a normal leaderstat, but when in the game I dont want the Health leaderstat to show on the top right corner, where player names and that stuff are located.

Then do not add health to leaderstats, you could make a different folder on the player named something other than leaderstats

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

	local hiddenstats = Instance.new("Folder", Player)
	hiddenstats.Name = "hiddenstats"
-- add health to leaderstats
	local Health= Instance.new("IntValue")
	Health.Name = "Health"
	Health.Value = 100
	Health.Parent = hiddenstats
-- now add money to the same leaderstats
	local money= Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 0
	money.Parent = leaderstats
1 Like

this is what i would have done for that:

local Players = game:GetService("Players")
local DS = game:GetService("DataStoreService")

local HDS = DS:GetDataStore("HealthData")

Players.PlayerAdded:Connect(function(Player)
	
	local ls = Instance.new("Folder", Player)
	ls.Name = "leaderstats"
	
	local coins = Instance.new("IntValue", ls)
	coins.Name = "Money"
	coins.Value = 0
	
	local health = Instance.new("IntValue", Player)
	health.Name = "PlrHealth"
	health.Value = Player.Character:FindFirstChildWhichIsA("Humanoid").Health 
	
	local data
	
	local success, errmsg = pcall(function()
		data = HDS:GetAsync(Player.UserId)
	end)
	
	if not success then
		warn(errmsg)
	end
	
end)

Players.PlayerRemoving:Connect(function(Player)
	local data
	
	local success, errmsg = pcall(function()
		data = HDS:SetAsync(Player.UserId, data)
	end)
	
	if not success then
		warn(errmsg)
	end
	
end)

i didnt exactly understand if the health value is the Health of the player or something else that youre planning to do with that

true but thats just optional, you can just put it under the player and done.

Thanks! Knew it was simple haha

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