How do I start with make a fps framework can control each single weapon?

I succeeded in making a fps framework that only works on “guns”,
but I want to make other weapons like melees, granades, and some skills maybe.

For example:

  • Melee has a little different mechanism than guns.
    When player click RMB, guns gets aimed. But it should do a secondary attack(like CS) on melees.

  • In the case of granade and skills, I will need to control the individual item. but don’t want to write the whole hundreds of lines of script for each.

I think it should connect functions for each tools like equip, activate, mouse button, key press…
but I have no idea how to. please help!

1 Like

I have no big skill in this but, u can make a hitbox part in front of character (which has cancollide = false, and transparency = 1), and then make a system when upon pressing desired button it checks if player touches the hitbox part, if player does: deal damage, if not: do nothing

1 Like

This isn’t what I was wondering about, but thanks for the answer!

I’m wondering how to create an fps framework that can implement multiple types of weapons (each of which behaves differently).

Maybe my bad english made some misunderstanding, sorry

1 Like

You need to separate your code more than in guns, the simpliest option is to find what guns and melee shares in common:

  • They can be equipped
  • They can be held by player
  • They react to inputs
  • They perform actions depending on inputs

This way we can create weapon as our base, remember that if you don’t plan to add option to create your own custom interactions, simple InputToFunction map will do, save it in tool data module

local InputToFunction = {
    [Enum.KeyCode.E] = "Use",
    [Enum.KeyCode.R] = "Reload",
    [Enum.KeyCode.Q] = "Mode"
}

Now, you can simply check what input you have received, but first of all, we need to get weapon

local Weapon = Player:GetCurrentWeapon()
if not Weapon then
    return
end

local ActionName = InputToFunction[Input]
if not ActionName then
    return
end
Weapon:Action(ActionName, ...)

Rest is making class for each gun or melee weapon, and adding action methood, it will call other methoods

function Gun:Action(ActionName: string, ...: any)
    if not self[ActionName] then
        return
    end
    self[ActionName](self, ...)
end

function Gun:Use(Hit: BasePart)
    print("Hitted:", Hit)
    self.Ammo -= 1
    if self.Ammo <= 0 then
        self:Reload()
    end
end
1 Like

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