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?
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 |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | Players.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 |
14 | 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
01 | local Egg = script.Parent |
02 | local Debounce = false -- starts at false |
03 |
04 | local Players = game:GetService( "Players" ) -- gets the players |
05 |
06 | Egg.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 |
15 | 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
01 | local eggs = game.ReplicatedStorage.Eggs |
02 |
03 | local spawns = game.Workspace.EggSpawns |
04 |
05 |
06 | while 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 |
Here's a better one:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | local DataStore = game:GetService( "DataStoreService" ) |
03 |
04 | local leader = Instance.new( "Folder" ,player) |
05 | leader.Name = "leaderstats" |
06 |
07 | for i,v in pairs (script:GetChildren()) do |
08 | local d = DataStore:GetDataStore(v.Name) |
09 | local x = Instance.new( "NumberValue" ,leader) |
10 | x.Name = v.Name |
11 | x.Value = d:GetAsync(player.UserId) or v.Value |
12 |
13 |
14 |
15 | end |
Then just add number values inside of the script with the names of leaderstats you want to add.
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?