You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Code a tool outside without putting any server side scripts in the tool itself. (script is inside serverscriptstorage btw)
What is the issue? Include screenshots / videos if possible!
Is there a more efficient / better way of doing this?
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I couldn’t find anything really explaining it but this was something I came up with.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
How would I make this more efficient / is there a better way of doing this?
local alreadyseen = {} -- Makes sure not to reapply the tool script when it's moved between player and backpack
game.Workspace.DescendantAdded:Connect(function(dc)
if dc:IsA("Tool") and dc.Name == "The First Bomb" and table.find(alreadyseen, dc) == nil then
firstbomb(dc) --function just would be the code for the tool itself if it was under it
table.insert(alreadyseen, dc)
end
end)
for _, dc in pairs(workspace:GetDescendants()) do
if dc:IsA("Tool") and dc.Name == "The First Bomb" and table.find(alreadyseen, dc) == nil then
firstbomb(dc)
table.insert(alreadyseen, dc)
end
end
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
I just use an UserInputService InputBegan event on the client, and then fire a RemoteEvent to the server to handle the actual logic. In the event on the server; I would write the code like the bomb’s duration until it explodes, and the bomb’s explosion itself.
I like making my system linear with an event/function between scripts, however what is right for your game depends on how you like to manage your code. It’s not really something other people can tell you.
The only improvement I can see here myself is that when checking the name of the descendant, you could instead make a table of functions, and then do something like tableOfFunctions [dc.Name] (dc). That way you don’t need to write elseif statements for multiple tools.
Actually another thing I noticed is that for the DescendantAdded event, you’re using game.Workspace instead of a more specific location. This is not that intuitive, and may reduce performance if you got a lot of parts spawning.
For the player tools, you can do game.Workspace.Players (a folder where I myself parent all the players’ character in the game). That way the DescendantAdded event has much less to check, but it will never miss the tools. For projectiles and abilities, you can make a folder for each, so game.Workspace.Projectiles and game.Workspace.Spells. It’ll be useful for other scripts as well. Being able to locate groups of items has helped me a decent amount over the course of development.
I’d probably write a DescendantAdded event on each folder, and then run the if dc:IsA("Tool") and dc.Name == "The First Bomb" and table.find(alreadyseen, dc) == nil then firstbomb(dc) table.insert(alreadyseen, dc) end
as a function for each of them.
It does increase the code a bit, but is also easier to read and understand it since you specify where you look for the tools.