Part Counter GUI Help, How do i make it? (Egg Hunt)

Hello Developers, I want to create an Egg Hunt experience, How do I create a GUI that counts how many eggs you have collected so far and saves data? You collect the eggs by touching them.

I have thought about using a regular LocalScript and making a

script.parent.Touched:Connect(function(hit)
    game.StarterGui.CounterGui.Text += 1
end)

please tell me if I put this in the right topic or if I have any errors and what should I do to make it?

I’ll make a simple version of this as a rubric you can follow.

A server script to handle creating the folder (placed inside of ServerScriptService):

-- Services
local Players = game:GetService("Players")

-- Variables
local EggsFolder = workspace:WaitForChild("Eggs") -- put the egg parts inside of here

-- Functions
local function newInt(a,b,c,d)
    local i = Instance.new(a)
    if c then i.Name = c end
    if d then for p, v in pairs(d) do i[p] = v end end
    if b then i.Parent = b end
    return i
end

local function onPlayerAdded(player)
    newInt("Folder", player, "EggsCollected")
end

local function onEggHit(eggName, hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
        local char = hit.Parent
        local player = Players:GetPlayerFromCharacter(char)
        if player and player:FindFirstChild("EggsCollected") then
            local EggsCollected  = player.EggsCollected
            if EggsCollected:FindFirstChild(eggName) == nil then
                -- Players first time finding this egg
                newInt("IntValue", eggName, EggsCollected)
            end
        end
    end
end

-- Events
Players.PlayerAdded:Connect(onPlayerAdded)

for _, egg in pairs(EggsFolder:GetChildren()) do
    if egg:IsA("BasePart") then
        egg.Touched:Connect(function(hit)
            onEggHit(egg.Name, hit)
        end)
    end
end

Somewhere on the client you’d now have to create an event to listen for any children added to the local player’s EggsCollected folder that we created on the server. That code would look something like this (this would go inside of a LocalScript under StarterPlayerStarterPlayerScripts:

local player = game:GetService("Players").LocalPlayer
local eggsCollected = player:WaitForChild("EggsCollected")

eggsCollected.OnChildAdded:Connect(function()
    -- Some function here would be to update your UI, like if you wanted to 
    -- visibly show which eggs you collected or how many
    
    -- For now I'm just going to print the number of eggs you collected & which
    
    print("You collected", #eggsCollected:GetChildren(), "out of 12 eggs!")
    
    -- Printing each one out via a for loop
    for _, egg in pairs(eggsCollected:GetChildren()) do
        print(egg.Name)
    end
end)

Note: The approach I took does not have data saving implemented. If you wanted to, you could look into ProfileService by loleris for saving data. It’s very easy to implement once you get over the first hill.

Also, another note: I know there’s other ways to handle this, such as storing which eggs each player has collected in a dictionary. But, this was the first way that came to mind.

1 Like