Question on how would i make tools using oop

Hello,
I would like to make a gun system, but idk how i would set it up. Ik OOP and ik module scripts, but how would it fit in together?

https://gyazo.com/a3ec666cfa23781b17678dd11a90e251

I have my guns ready, but should i make like module scripts for everything, so basically hit scanning, get character etc etc. Or should i make like scripts inside of the guns to make it specifically for those. But thats not my plan. I want it so i can make changes easily.

I may made everything worse to understand, but what i want is:
How i would make a oop based gun system, and easy customisible.

Reason for this sudden change is because i always used to make like 435^44 scripts in like 1 tool and i would copy all of them and throw them in the second and so on. Whenever i needed to update my “system”, i usually deleted every script, updated 1 pair, and i went on and copy paste everything back. This was a big hastle and now im here.

Definitely use modules. Modules will make your life so much easier when trying to update your code.
I would have a module called “Damage” and every time the player attempts to damage another player, use the module to handle the request.

local Module = require(game.ServerScriptService.Damage)
Module.Damage(plr, target, anythingelsehere)

The module would then handle the request and subtract the appropriate damage from the target. As for the gun system itself, there are lots of articles online that can help.

Yes, i thought of that. But should i make 1 server sided script, and paste it somewhere in serverscript service?

What I would do is have a single script in the serverscriptservice that could be copied into each gun, that way you only have to edit one script. The script itself wouldn’t do anything but send information to the Damage module.

1 script i have to copy in each gun :confused:

Comes down to

Maybe i just understood wrong, maybe you meant local script or such

You would copy the script via another script, and parent it to all of the guns.

for _, v in pairs(game.ServerStorage.Guns:GetChildren() do
local clone = game.ServerScriptService.GunScript:Clone()
clone.Parent = v
end

This means you only need one script, which makes it a lot easier.

Smart guy, but isnt a whole module script dedicated to damage not like over exaggerated?
I mean, i could always add more stuff to it, like vfx what ever.

What do you mean by over exaggerated?

Making a module just for damage seems exaggerated, but like i said:

So ignore my past statement

No, I don’t think so. You may be surprised by how many things you may use the Damage module for. For instance, if you had multiple teams, your script would need to detect whether the player and the target are on the same team. Another reason you would need a damage module is to give the player some type of reward for damaging/killing another player.

Good point.
I will try out the serverscript thingy and yeah, we will see.

Should i do the same for the Client? I feel like it.

You will need a local script if you are using a RemoteEvent.

Yeah ofc i would need the mouse, how else would i let the server know where to shoot? Seems like you have something else in mind. Mind sharing?

The only way i know is by sending a remote event and passing in the position, instance etc etc.

And animations will come in handy i think

I recommend using raycasting for the actual shooting, to let the server know where to shoot you would need a RemoteEvent that passes the Mouse 3D target position to the server.

I always use raycasting, but you need a direction to cast to, how would you get it anyway?

Btw, i got it working:

local SS = game:GetService("ServerScriptService")
local RS = game:GetService("ReplicatedStorage")

local Damager = require(SS.Guns.Modules.Damager)

local remotes = RS.Guns.Remotes
local shot = remotes.Shoot

shot.OnServerEvent:Connect(function(plr, hit:Instance, tool:Tool)
	local fireRate = tool:GetAttribute("FireRate")
	local Damage = tool:GetAttribute("Damage")

	if fireRate and Damage and typeof(hit) == "Instance" then
		Damager.Damage(hit, Damage)
	end
end)

Result: https://gyazo.com/597aaf3fa3e21dfd7f8c8f9c9efcd2bd

Anyways, i couldve thought of this myself, and i will further expand this.

Use the mouse’s target CFrame.

Nah, im too good for that (im obv joking)

I do a cast from the unitray of the camera in the direction of the unitray

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.