Local script inside the tool is a waste of memory?

Do you think this is a waste of memory if I put local script inside this tool.
Idk if its a waste of memory from the server or client.

Let’s say: “I want to change the code of every single weapon cause its more optimized than previous.”
I don’t want to repeat the process again and some weapons that I forgot to change it because there’s a lot of weapons I need to change.
This is what inside this replicatedstorage > weapons. I don’t want to repeat this cause Im going to add more weapons later: (And btw, this is a ripoff version of mm2 but…)
Explorer picture

Don’t talk about a raygun from sheriff Its just for experimental or fun thing ok?

Now, Imma repeat this question. Do you think local script inside the tool is a waste of memory from the server or client side?
If so, should I do another method by using childadded event to apply the function to the tool or making a inventory system from scratch?
If you aren’t getting it nor understand, I tried my best to describe or something explanation here.

6 Likes

You can use CollectionService to make the local scripts go to all of the weapons

-- local script
local CollectionService = game:GetService("CollectionService")

local melee = CollectionService:GetTagged("Melee")
local shooting = CollectionService:GetTagged("Shooting")

for i,tool in melee do
-- put the code in the 'Melee' script here
end

for i,tool in shooting do
-- put the code in the 'Shooting' script here
end

you can add tags to the tools by going into their properties, scrolling all the way down and typing in “Shooting” on guns and “Melee” on knives. (remember to replace ‘script’ with ‘tool’ such as ‘tool.Parent’ instead of ‘script.Parent’)

5 Likes

It might be work tho. Ill might test it and Ill reply to you later.

5 Likes

I don’t think it will work because I need a player to receive a weapon when the round starts.
On my server script:
Tool:Clone().Parent = Player's backpack.
I tried to use TagAdded.

CollectionService.TagAdded:Connect(function(Tag)
	print(Tag)
	local Tagged = CollectionService:GetTagged(Tag)
	for i, tool in pairs(Tagged) do
		if tool:IsA("Tool") then
			print("Its a tool")
			tool.Activated:Connect(function()
				print("Hit")
			end)
			tool.Equipped:Connect(function()
				print("Equipped")
			end)
		end
	end
end)

It didn’t work because the tool is cloned and added in my inventory and didn’t fire the tagadded. Idk what happened because of cloned tool. So equip and activate event won’t work.
Imma try something a childadded event.
Idk if im stupid or something.

2 Likes

the script provided by dogoably just puts the local scripts in the guns, so i dont see why it would change the fact you want the player to receive a weapon when the round starts

2 Likes

tag added fires when you assign an instance a certain tag, not when you clone a tool with a tag, maybe i am misunderstanding something you said?

2 Likes

So… Only fires when assigning an instance with tag but didn’t fire when cloned with a tag.
I think I get it.
But Im just questioning about a local script inside the tool is a waste of memory from server/client.

3 Likes

if using local scripts in a tool was a waste of memory, then how would you do it? thats the only way to do it, you cant do it in another way, because tool events like equipped and activated only work client-side, meaning inside a local script

3 Likes

Then what about childadded event inside the starterplayerscript?

3 Likes

what do you mean by a childadded event inside a starter player script?

2 Likes

You can use Backpack.ChildAdded:Connect() and then you can do Tool.Equipped:Connect(). But when you unequipped tool, the childadded event fires and stacking the equip connection. If you unequip then equip again like 500 times, its a memory leak.
Found this one btw:

2 Likes

if it doesnt work, why think about it? and if youre making guns, just put the local script inside it. By the way, the local script is called local because it runs locally, so it wont affect the server in any way, except the communication to it (for example spamming remote events)

2 Likes

and talking about the question in the title, even if you want to replace the gun script in all the guns with a more optimized one, the guns would still need that local script, and its not a waste of memory to have a local script inside a tool if it does something, same for the server. yea its a waste to have scripts lying around doing nothing, even if the resources used are little, but if they have a purpose, you cant call them a waste, unless youre not doing something efficiently and optimized (for example having a ton of local scripts for detecting separate user inputs instead of making it all in one)

1 Like

make a shared code (Module Scripts for example) so these tools can utilise it, for ranged weapons, their ranged code, for melees their melee code and stuff, making the only stuff to change its a few lines of configuration.
This is basically one of the principles in SOLID (should learn about that, it tells how to build a future proof project)

2 Likes

replying to second question about the waste of memory
The answer is simple - No, its not a waste, every script instance its just a different thread, just that.
Every script consumes as much resources as it need (even if you put your entire codebase under one script, it will consume same amount of resources)

1 Like

alright, nice reply, good to know

1 Like

Hm but which option is better? The CollectionService method given by dogoably seems quick and easy to apply but the ModuleScript method seems to be more complex but probably more efficient?
While I do prefer doing the ModuleScript method, don’t they do practically the same thing?

Do whatever fits your needs, there is no right or wrong solutions.
Ask yourself some questions on how you want your system to work, what future updates it should handle and more.
After asking yourself a right question, you can find a right solution.