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.
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.
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.
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.
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)