Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I add leaderstats in a server script? [closed]

Asked by
MattVSNNL 620 Moderation Voter
4 years ago

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?

0
Request. Dovydas1118 1495 — 4y

Closed as Not Constructive by Dovydas1118, RazzyPlayz, virushunter9, and Soban06

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

3 answers

Log in to vote
1
Answered by 4 years ago

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.

01-- Insert into ServerScriptService as a ServerScript
02local Players = game:GetService("Players")
03 
04Players.PlayerAdded:Connect(function(Player)
05 
06    local stats = Instance.new("IntValue")
07    stats.Name = "leaderstats"
08    stats.Parent = Player
09 
10    local Eggs = Instance.new("IntValue")
11    Eggs.Name = "Eggs"
12    Eggs.Value = 0
13    Eggs.Parent = stats
14end)

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

01local Egg = script.Parent
02local Debounce = false -- starts at false
03 
04local Players = game:GetService("Players") -- gets the players
05 
06Egg.Touched:Connect(function(hit)
07    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.
08 
09    if not (Debounce) then -- checks if the Debounce is false
10        Debounce = true -- if it's false it will set it to true
11        Player.leaderstats.Eggs.Value += 1 -- Adds 1 every time it is touched
12        wait(1) -- cooldown before the debounce is set back to false for the script to be able to be used again
13    Debounce = false
14    end
15end)

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!

Ad
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
4 years ago
Edited 4 years ago

My script is

01local eggs = game.ReplicatedStorage.Eggs
02 
03local spawns = game.Workspace.EggSpawns
04 
05 
06while wait(2) do
07 
08    local eggsTable = eggs:GetChildren()
09 
10    local randomEgg = eggsTable[math.random(1,#eggsTable)]
11 
12    local spawnsTable = spawns:GetChildren()
13 
14    local randomSpawn = spawnsTable[math.random(1,#spawnsTable)]
15 
View all 34 lines...
Log in to vote
0
Answered by
Gogulsky 129
4 years ago

Here's a better one:

01game.Players.PlayerAdded:connect(function(player)
02local DataStore = game:GetService("DataStoreService")
03 
04     local leader = Instance.new("Folder",player)
05leader.Name = "leaderstats"
06 
07for i,v in pairs(script:GetChildren()) do
08local d = DataStore:GetDataStore(v.Name)
09local x = Instance.new("NumberValue",leader)
10x.Name = v.Name
11x.Value = d:GetAsync(player.UserId) or v.Value
12 
13 
14 
15end
View all 26 lines...

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