Im creating a tool system using OOP (type of tool is irrelevant), and I have a server script that handles all of the module’s functions. I also have a local script in StarterPlayerScripts, and im not super confident I know what to put in there, but anyway, the tool has RemoteEvents inside of it such as an “Activated” event. Im wondering how to detect when a player with one of these tools fires that event, from a server script. Btw, The server script is in ServerScriptService, not the tool (for efficiency/optimization purposes), so I couldn’t just access the remote events like that. Ive considered using CollectionService to handle the different kinds of events through tags, but I don’t know if that’s the best way to do it.
just put the modulescripts inside of the tool instead and have the remote events be in replicated storage, then when fired u have all the information about the tool they’re wielding without passing anything into the remote.
Well, I wouldn’t want to have copies of the module scripts inside of every tool. Also, I don’t really understand how this solves my problem of figuring out how to handle remove events that are in tools, from a script in ServerScriptService.
well then u can have all of ur tools in replicated storage and connect all the events there, when u clone the tool all of the connections will be cloned with it I’m pretty sure
Im confused, inside of what script would I connect all of the events? The local script? Because the local script isn’t in the tools, its in starterplayerscripts.
I don’t understand why u’re overcomplicating urself so much when the modulescript solution is way easier to reason about
but if u really want to follow through with this approach although it’s really not a good idea, u would want to connect the server sided connections on the server script and the client connections on the localscript, just loop through the tools and connect each event to a function
Ok I don’t think were on the same page so let me just explain my situation again in a bit more detail:
-I have a module script in ReplicatedStorage with functions like toolModule.Create(), toolModule:Init(), toolModule:Activated().
-My tool has remote events inside of it, such as “Activated”
-I have a local script in StarterPlayerScripts which is supposed to handle all of the tool’s mechanics from the client side of things.
-I have a server script in ServerScriptService that is supposed to handle every type of that tool that any players might have. This is where the problem lies. I don’t know how to detect the .OnServerEvent() event for however many of these tools there might be, since I’m not sure how to access a player’s tool’s remote events from the server.
Sorry, I may be overcomplicating this Im really just trying to do everything as optimized as possible. Im fairly new to OOP and this kind of system in general, so forgive me if I misunderstand something simple.
Edit: When I say tool, im talking about a specific type of tool that I don’t have a name for, im not talking about any kind of “Tool” instance.
The point of OOP is to prevent copy pasting a bunch of module scripts inside models/tools. Asking the OP to copy paste a bunch of Modules into the tools literally defeats the purpose of using OOP.
Also, if the OP doesn’t want your to use your solution you shouldn’t be demeaning him for that. He’s likely doing more complicated stuff in his game and doesn’t have the time to be replacing his already working system
Sorry if I didn’t provide enough information initially, I hope I was able to explain myself a bit better in my most recent reply. Im going to say this again, but could I just use collection service ? Like assigning different tags to each of the different events and then getting them in the server script that way
okay so I’m confused as how u don’t know how to access a player’s tool’s remote events from the server, when the server should be able to see all backpack items and the wielded item
and yeah using collection service for every tool is a feasible solution if u assign different tags for each tool
You don’t need to move your server script into the tool itself – you can still handle those RemoteEvents cleanly from one central script in ServerScriptService.
The key is that when a player equips a tool, that tool instance (and its descendants, including RemoteEvents) are parented into the player’s Backpack or Character. From the server, you can detect new tools being added and then hook their RemoteEvents.
Example:
local Players = game:GetService("Players")
local function hookTool(tool: Tool)
-- Find the RemoteEvent inside the tool
local remote = tool:FindFirstChild("Activated")
if remote and remote:IsA("RemoteEvent") then
remote.OnServerEvent:Connect(function(player, ...)
-- Handle whatever this remote is supposed to do
print(player.Name .. " fired " .. remote.Name .. " from tool " .. tool.Name)
-- pass args into your OOP handler, etc.
end)
end
end
local function onCharacterAdded(char)
-- hook tools that already exist
for _, tool in ipairs(char:GetChildren()) do
if tool:IsA("Tool") then
hookTool(tool)
end
end
-- listen for tools added later
char.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
hookTool(child)
end
end)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(onCharacterAdded)
end)
Alternative approaches
CollectionService tags are nice if you want to tag tools and manage them generically. You’d still follow the same principle: connect to their RemoteEvents when they appear.
Modules for logic are fine too – you can require a module per tool type in your main server script, and just dispatch to the right one inside the OnServerEvent handler.