How to implement special functionality to weapon system?

One of those theorethical questions, i want to create weapon system and i need to add special functionality for some weapons, such as gun reloading when pressing R or dashing with a sword when it was clicked 4 times

Problem is that it should be expandable upon later with updates, and i have no idea what i can do to handle those special cases from remote event, my only guess is some sort of composition where i will pass tool’s name and then additional stuff, but is there any better solution?

You could try putting the special event code into a ModuleScript. This approach makes it easier to manage and update.

For handling key presses, you can use ContextActionService. Here’s an example:

If you want the function to fire when the key is pressed four times, you can do something like this:

local pressed_number = 1

pressed_number = pressed_number + 1

if pressed_number > setnumber then
--do something here
end

contextActionService:BindAction("Reload", reloadfunction, false, keycode)
1 Like

good idea, but as i mentioned i have problem with determining it on server, and context action works on client only

use a remoteEvent and fireserver whenever the key is pressed.

no, again i have problem with handling special inputs, some weapons might have simply left click and this is default one, others might have additional functions like reloading or smth and this is where i have problem

Why not use OOP inheritance?

--generic gun base
local base = {}
base.__index = base

export type base = setmetatable({} :: {
   ammo : number,
   maxammo : number
}, base)

--some constructor
function base.new(): base
 return setmetatable({
  ammo = 10,
  maxammo = 10
 }, base)
end

--some reload fn (should prob be in derrived)
function base:R()
   ammo = maxammo
end

--some primary fn like sword swing or gun shoot
function shoot()
print("pew")
end

return table.freeze(base)

And a derrived class:

local base = require()--base

local derrived = setmetatable({}, base)
derrived.__index = derrived

function derrived.new()
  return setmetatable({
    ammo = 10,
    maxammo = 10
   }, derrived) --or some special behaviour
end

function derrived:SomeSpecial()
print("boom")
end

return table.freeze(derrived)

And then:

local derrived = require()--derrived
local inst = derrived.new()

--base class fn, works
inst:R()
--derrived class
inst:SomeSpecial()

Now you just make an inheritance graph, like make a base class of fns that all weapons share, then implement derrived classes with their special traits. Let derrived classes override base fns like shoot to implement their own logic like sword swing or gun shoot or whatever.

Hope this helps.

hmmm good idea, but how to handle those special inputs, like let’s say i send string with interaction name, and what to do with it for specific function to work?

Well you can just index the table with your string:

--then call the result using ()
derrived["Reload"]()

Or you can name every fn in the base & derrived classes to the input they are bound to, then just index by input name.

--shoot fn
function base:MouseButton1()
end

--some code that results in input
base[Inputobj.KeyCode.Name]()

--use EnumItem.Name
--eg: Enum.Keycode.E.Name
1 Like

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