Collecting multiple coins & regenerating them using 1 script in SSS?

Recently I’ve been working on a game that involves collecting jewels (“Coins”), and while I was doing this, I realized that having a script in each jewel to fire to another script using a bindable function was a fairly poor choice to do.

image
in each jewel, I have it so if the player touches it, the bindable function fires over the amount that that particular jewel possesses over to the data store then deletes itself for around 60 seconds then re appearing,

but what I want to do is have just one script that affects maybe a folder of jewels, each time a jewel is touched , it sends over the amount via bindable function and collected on the datastore script.

How would I go about doing this?

Thanks
(sorry if my wording is slightly weird, I don’t know how to go about this)

You can use :GetChildren() on the Folder to retrieve all Children and listen to Folder.ChildrenAdded (This is an event!!) to get all newly added parts and then bind them to the Touched Listener.

local Folder = workspace:FindFirstChild("Jewels") -- This is your folder

function AddListener(part)
    part.Touched:Connect(function(hit)
        -- The code you want to execute on hit
    end)
end

for index, foundPart in pairs(Folder:GetChildren()) do
    AddListener(foundPart)
end

-- WARNING: THIS GETS CALLED ON ALL NEW CHILDREN, NO MATTER WHAT IT IS
Folder.ChildAdded:Connect(function(newChild)
    if newChild:IsA("BasePart") then
        AddListener(newChild)
    end
end)
2 Likes

Wow bro, thanks again lmao! Your a huge help

1 Like