Replicate Local Script Codes

Example, I got multiple tools and all of them contain local script.
image

Inside the local script.

local Number	= 10

local Tool	= script.Parent

local function Print()
	print(Number)
end

Tool.Activated:Connect(Print)

Let’s say I want to change the codes and don’t want to repeat for all of them and also each of the tool have different Number value.

Is there any methods to do this?

You didn’t explain clearly but I’m assuming you want the Number value to be randomized?
If so, you should use math.random.

Math.random takes 2 arguments, a minimum number and a maximum number.
The min value determines the starting range.
The max value determines the end range.

If you have min as 1 and max as 10, you will get a random number between 1 and 10.
If you wish to use decimals in your randomizer, use Random.new():NextNumber(min, max) or divide the random values.
Example 1:

Random.new():NextNumber(0, 1) -- returns a decimal between 0 and 1, e.g. 0.35

Example 2:

math.random(0, 10) / 10 -- returns a decimal between 0 and 1, e.g. 0.3

Im sorry for the bad explaination, I dont want the Number to be randomized.

The point is all the tools have the same local script with same codes. Let’s say if I want to change the code, I need to change all of them one by one again. The question, is there any method that I can just change only one local script and then the other local script follow the changes but the Number value is not changed?

So… you want to edit one script and have all the other scripts update but with the variable Number staying as 10 or whatever value you set?

1 Like

Correct, that what I’m looking for.

I would move the code from the scripts into a module in ReplicatedStorage then require the module in the local scripts. In the module create a function containing the code that is repeated and call it from the local script via the connected event passing whatever data the function needs to work on.

1 Like

You cant really do that but you could try using attributes or values.

Attributes are like custom properties and are usually found at the bottom of your selected instance.
Maybe set an attribute like “Number” to 10 for each tool and have each script call GetAttribute?

Example:

local Tool = script.Parent

local function Print()
    local number = Tool:GetAttribute("Number") -- this returns the 'Number' attribute of the tool
	print(number)
end

Tool.Activated:Connect(Print)

Otherwise, I suggest following @BootneckTech’s method.

Tested it, and it works >

repeat wait() until #game.Players.LocalPlayer.Backpack:GetChildren() > 0
wait()
for i, Tool in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
	local Number	= 10
	Tool.Activated:Connect(function()
		print(Number)
	end)
end

Put this inside of a local script located in StarterPlayerScripts

If you wanna know what this does:
This repeats ‘wait()’ until the amount of instances in the players backpack is greater than 0, then it delays again to allow time for the rest of the tools to insert, after that it loops through all of the tools in the players backpack, and assigns the function to it.

Sorry but I don’t think anyone here except this answer understand what does the problem means. Not to be mad at anyone though. Also there is already a solution:

Here’s a example code incase you can’t understand what does that mean, module will be in ReplicatedStorage and the Local script will be inside the tool. Both script names are default (ModuleScript and LocalScript)

Module:

local Module = {}

function Module.printNumber(number)
    print(number)
end

return Module

Local:

local Number = 10

local Tool = script.Parent

local function PrintNumber()
    require(game:GetService("ReplicatedStorage").ModuleScript).printNumber(Number)
end

Tool.Activated:Connect(PrintNumber)

Output:
10 (From the local script)

You could modify what will the function will do in the ModuleScript. You have to pass arguments to the function if there is variable where local script have but module doesn’t. Maybe try figure out why will it prints 10!

1 Like

Sorry for the late reply, this is what I’m looking for and thank you so much!

1 Like