Need help with OOP Gun System

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.

Here’s the local script that loads the framework:

local replicatedStorage = game:GetService("ReplicatedStorage")
local modules = replicatedStorage:FindFirstChild("Modules")
local gunInfo = modules:FindFirstChild("GunInfo")

local gunClient = require(modules:FindFirstChild("GunClientModule"))

local tool = script.Parent
local gunInfo = gunInfo[tool.Name]

gunClient.new(tool, gunInfo)

This isn’t how you do Object Orientated Programming, you need to do this in a module, and you need to make sure you use the proper meta functions, which I can’t tell if you are.

Try reading this post All about Object Oriented Programming

Well either way, are you able to solve my problem? Like why the information gets overwritten? That’s what I need help with anyways.

Wait I know what you’re telling me to do. Thank you

Sorry to keep bothering you, but would this be the correct way now?

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

	return newTable;
	
end

function GunFramework:Shoot(mousePos, character)
	
	print(self)
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = 
		{
			character,
			workspace.Ignore

		}

	local origin = character.Head.Position
	local pos = mousePos

	local direction = (pos - origin).Unit*self.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
	
end
1 Like

if GunFramework.__index is itself then yes

Just make sure,

GunFramework.__index = GunFramework
1 Like

Yes it already is like that I just cut it out of the code I showed

image

Thank you for saving me from hours of searching and fixing my gun module later on. Have a good rest of your day/night.

1 Like

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