I have 10 different objects that all use the same script. If I was to change something in it, I would have to go and change it for all remaining 9 objects. This is kinda time consuming and I feel dumb doing that.
Is there a way to 'share' scripts between multiple object? Simillar to the way popular game engines work.
Yes. There is. I will give a specific example. If it does not apply to what you want, oh well.
Say you have multiple parts and you want them to be able to be clicked. So you use ClickDetector.MouseClick
. When they are clicked, they give you points.
-- Assume hierarchy is Workspace.Coins
local coins = script.Parent -- coin parent is a model Workspace.Coins coin.MouseClick:Connect(function(player) player.Coins.Value = player.Coins.Value + 1 end)
This is the same script you have for your 40+ coins. But say later on you decide you want to double how many coins they get based on if they have a coin doubler tool in their backpack.
So you want to check if they have that tool.
coin.MouseClick:Connect(function(player) local coins_to_add = player.Backpack:FindFirstChild("CoinDoubler") and 2 or 1 player.Coins.Value = player.Coins.Value + coins_to_add end)
But you have to change that code for all 40+ scripts with this!
What you can (and should) do, is instead put it in the model of coins in the coins model itself with all the coins and change to a for loop:
-- All in 1 script! local function on_mouse_click(player) local coins_to_add = player.Backpack:FindFirstChild("CoinDoubler") and 2 or 1 player.Coins.Value = player.Coins.Value + coins_to_add end for _, child in ipairs(game:GetService("Workspace").Coins:GetChildren()) do if child.Name == "Coin" then child.ClickDetector.MouseClick:Connect(on_mouse_click) end end
You can name the tens object with the same name and add 1 at the end of the 1st object's name then 2 to the 2nd object's name and so on. then you can make a loop and use the " i " to find each objects like this:
for i = 1, 10 do game.Workspace:WaitForChild("whatever you named your part"..i).(Whatever you wanna do with those 10 parts) end
hope it helped