Issue with leaderstats

Goal | When a player touches an egg, their leaderstat goes +1.

My scripts:
This script below spawns the eggs and destroys the egg if it is touched. The remoteEvent is just to trigger the GUI, but I won’t show that script



local eggs = game.ReplicatedStorage.Eggs
local spawns = game.Workspace.EggSpawns

while wait(2) do
	
	local eggsTable = eggs:GetChildren()
	local spawnsTable = spawns:GetChildren()
	local randomEgg = eggsTable[math.random(1,#eggsTable)]	
	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)
			eggClone:Destroy()
		end
	end)
	
end 

Leaderstats, server script

	
	local Players = game:GetService("Players")

	local function leaderboardSetup(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

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

I tried adding a touched event in the leaderstats script but it didn’t work. I also tried making a remote event that was called in the first script and carried onto the second script but that also did not work

Help would be great, thank you!

1 Like

When you try to execute this script, does it display an error message of some sort?

Nope! Everything is working normally, but I just want the leaderstats to go up when I touch an egg.

just put after the if plr then:

plr.leaderstats.Eggs.Value = plr.leaderstats.Eggs.Value + 1

So it’s like:

if plr then
    plr.leaderstats.Eggs.Value = plr.leaderstats.Eggs.Value + 1
end
1 Like
plr.leaderstats.Eggs.Value += 1

May as well use the addition compound operator.

1 Like