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

How do I add leaderstats in a server script?

I'm making an egg hunt game and I'm trying to make when you collect an egg it adds 1 egg leaderstat How do I make that?

1 comment

0

Request.

3 answers

Please do take note that ScriptingHelpers is NOT a Request Site! It is for help with scripting only. I suggest attempting to learn at least the bases of scripting, or, there are some really good youtube tutorials about these things on YouTube!

With your case, it's actually very simple to do this.

-- Insert into ServerScriptService as a ServerScript
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)

    local stats = Instance.new("IntValue")
    stats.Name = "leaderstats"
    stats.Parent = Player

    local Eggs = Instance.new("IntValue")
    Eggs.Name = "Eggs"
    Eggs.Value = 0
    Eggs.Parent = stats
end)

From the script above, you have just created a leaderstats script, and now we are ready to get started to add values to this leaderboard. Since we have created an IntValue as our Eggs value, we can simply just add 1 to the Value per every egg collected. From this, it's quite simple to add this, it's just about adding values every time an egg is touched.

Every egg needs a script to tell the leaderboards to add a point per egg collected. It's very easy to do this via a .Touched event triggered when the egg is touched. To make sure that your egg doesn't collect points multiple times at once, I'll be using a Debounce, you can learn more about it here: https://developer.roblox.com/en-us/articles/Debounce

local Egg = script.Parent
local Debounce = false -- starts at false

local Players = game:GetService("Players") -- gets the players

Egg.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent) -- since we are using a serverscript we cannot get the player via a LocalPlayer function, so we will get the player via the part being detected instead.

	if not (Debounce) then -- checks if the Debounce is false
		Debounce = true -- if it's false it will set it to true
		Player.leaderstats.Eggs.Value += 1 -- Adds 1 every time it is touched
		wait(1) -- cooldown before the debounce is set back to false for the script to be able to be used again
	Debounce = false
	end
end)

Keep in mind, all of this will not save after a player leaves, for that you will need to setup a DataStore, but I'll let you learn that for yourself!

I hope this helped you, if it has then feel free to accept the answer, this will grant points for both me and you!

Hope this helped, have a good day!

My script is

local eggs = game.ReplicatedStorage.Eggs

local spawns = game.Workspace.EggSpawns


while wait(2) do
	
	local eggsTable = eggs:GetChildren()
	
	local randomEgg = eggsTable[math.random(1,#eggsTable)]
	
	local spawnsTable = spawns:GetChildren()
	
	local randomSpawn = spawnsTable[math.random(1,#spawnsTable)]
	
	
	local eggClone = randomEgg:Clone()
	eggClone.Parent = game.Workspace.SpawnedEggs
	
	eggClone.Position = randomSpawn.Position + Vector3.new(0,20,0)
	eggClone.Anchored = false
	
	eggClone.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if plr then
			game.ReplicatedStorage.FoundEgg:FireClient(plr,eggClone.Name)
			game.Workspace.EggSound:Play()
			eggClone:Destroy()
		end
		
	end)
	
end

Here's a better one:

game.Players.PlayerAdded:connect(function(player)
local DataStore = game:GetService("DataStoreService")

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

for i,v in pairs(script:GetChildren()) do 
local d = DataStore:GetDataStore(v.Name)
local x = Instance.new("NumberValue",leader)
x.Name = v.Name
x.Value = d:GetAsync(player.UserId) or v.Value



end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(script:GetChildren()) do 
		print("Getting")
		local DataStore = game:GetService("DataStoreService")
		local d = DataStore:GetDataStore(v.Name)
		d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
		print("Saved")
	end
end)

Then just add number values inside of the script with the names of leaderstats you want to add.

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