Heya There!!
I’ve made this simple gun module that handles core mechanics of guns such as firing and reloading. The issue is that I’m struggling on finding a way to make guns with the module system implemented in. Should I have a script in ServerScriptService that detects if a player has an item equipped and is using it or something else?
--[[SERVICES]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
--[[MODULE]]--
local BaseGunModule = {}
BaseGunModule.__index = BaseGunModule
function BaseGunModule:CreateGunStats(Damage, Firerate, Range, AmmoHold, AmmoContain)
local self = setmetatable({}, BaseGunModule)
self.Damage = Damage
self.Firerate = Firerate
self.Range = Range
self.AmmoHold = AmmoHold
self.AmmoContain = AmmoContain
--//Adding weapon functions
self:Equip()
self:OnUse()
return self
end
--//Weapon functions
function BaseGunModule:Equip()
--//TBA
end
function BaseGunModule:OnUse()
if self.AmmoHold >= 0 then
self:Fire()
else
self:Reload()
end
end
--//Firing & Reloading
function BaseGunModule:Fire(A, B) --//A is where the shot is first positioned. B is wherever the shot hits at.
local NewRaycast = workspace:Raycast(A, B * self.Range)
local RaycastInstance = NewRaycast.Instance
if RaycastInstance then
--//Finding to see if whatever we hit had a humanoid in it.
local Humanoid = RaycastInstance.Parent:FindFirstChildWhichIsA("Humanoid")
if not Humanoid then
return
end
if Humanoid.Health >= 0 then
Humanoid:TakeDamage(self.Damage)
print(RaycastInstance.Parent.." has taken "..self.Damage.."!")
end
end
end
function BaseGunModule:Reload()
--//TBA
end
return BaseGunModule