This is my first time using OOP to make a gun system sorry if I’m doing it all wrong and it makes you mad.
My issue is that all the data gets overwritten by the next gun that loads the framework.
My code:
function GunFramework.new(toolInstance : Tool, configInstance)
local newTable = {}
setmetatable(newTable, GunFramework)
newTable.Instance = toolInstance;
newTable.Config = require(configInstance); -- assuming your config is a module containing the gun data
newTable.Values = values
local sounds = newTable.Config.Sounds
local plr = players.LocalPlayer
local mouse = plr:GetMouse()
local character = plr.Character
local humanoid = character:FindFirstChild("Humanoid")
local animator = humanoid:FindFirstChild("Animator")
local animations = newTable.Instance:FindFirstChild("Animations")
local animationsTable = loadAnimations(animator, animations)
newTable.Instance.Equipped:Connect(function()
animationsTable.Equip:Play()
animationsTable.Idle:Play()
newTable.Values.Equpping = true
newTable.Values.Equipped = true
end)
animationsTable.Equip.Stopped:Connect(function()
newTable.Values.Equpping = false
end)
newTable.Instance.Unequipped:Connect(function()
animationsTable.Equip:Stop()
animationsTable.Idle:Stop()
newTable.Values.Equipped = false
newTable.Values.Mouse1Down = false
end)
mouse.Button1Up:Connect(function()
if newTable.Values.Equipped then
newTable.Values.Mouse1Down = false
end
end)
mouse.Button1Down:Connect(function()
if newTable.Values.Equipped then
newTable.Values.Mouse1Down = true
end
end)
runService.RenderStepped:Connect(function()
if newTable.Values.Mouse1Down and not newTable.Values.Equpping then
if newTable.Values.Equipped then
if not newTable.Values.Shooting then
if humanoid.Health > 0 then
newTable.Values.Shooting = true
if (newTable.Config.Gun.Type) == "Semi" then
newTable.Values.Mouse1Down = false
end
local params = RaycastParams.new()
params.FilterDescendantsInstances =
{
character,
workspace.Ignore
}
mouse.TargetFilter = character
local origin = character.Head.Position
local pos = mouse.Hit.Position
local direction = (pos - origin).Unit*newTable.Config.Bullet.Range
local raycast = workspace:Raycast(origin, direction, params)
local intersection = raycast and raycast.Position or origin + direction
local raycastTable = {}
if raycast then
raycastTable.Instance = raycast.Instance
raycastTable.Normal = raycast.Normal
raycastTable.Position = raycast.Position
raycastTable.Distance = raycast.Distance
end
animationsTable.Shoot:Play()
remotes.Shoot:FireServer(intersection, newTable.Instance.MuzzlePart.Position, newTable.Config.Bullet, raycastTable)
shootsoundRemote:FireServer(sounds.Shoot, sounds.DistantShot)
wait(newTable.Config.Gun.Cooldown)
newTable.Values.Shooting = false
end
end
end
end
end)
return newTable;
end
I have two guns and the first gun works on it’s own, but when the second gun loads, it uses all the second gun’s animations and sounds and config and all that.