Should I handle weapons on server or client?

Not sure if this would be classed as the right category for this. If it’s not let me know and I will change it.

So I am working on a game, currently working on making the weapons, I am using Knit and RaycastHitboxV4

I currently have 1 server script and 3 folders; Components, Modules, and Services. Now I didn’t know whether the weapons would be classed as a Service or Components. I ended up putting it into components since it will apply stats and connections to the swords as they are added.

Weapons Component Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Knit = require(ReplicatedStorage:WaitForChild("Common"):WaitForChild("Knit"))
local Janitor = require(Knit.Util.Janitor)
local RaycastHitbox = require(ReplicatedStorage:WaitForChild("Common"):WaitForChild("RaycastHitboxV4"))
local DataValues = require(script.Parent.Parent:WaitForChild("Module"):WaitForChild("DataValues"))

local Weapons = {}
Weapons.__index = Weapons

Weapons.Tag = "Weapon"

function Weapons.new(instance)
    local self = setmetatable({}, Weapons)
    self._janitor = Janitor.new()
    self.Stats = DataValues[instance.Name]

    local newHitbox = RaycastHitbox.new(instance)

    self.Connections = {
        ["Attack"] = instance.Activated:Connect(function()
            print("Used "..instance.Name)
            newHitbox.OnHit:Connect(function(hit, humanoid)
                print(hit.Name)
                humanoid:TakeDamage(self.Stats.Damage)
            end)
            newHitbox:HitStart()
        end)
    }

    self._janitor:Add(function()
        print("Weapon Destroyed")
    end)

    return self
end

function Weapons:Init()

end

function Weapons:Deinit()

end

function Weapons:Destroy()
    self._janitor:Cleanup()
end

return Weapons

My questions are:

Would it be bad to run all of this on the server (including the animations, sound, damage)?

Are there any bad practices/ lines of code?


Thank you and I feel like this post will get mixed reactions since it’s not a problem just a bit of guidance. I appreciate everyone that has read this, this is my first project using Knit and other APIs.

2 Likes
  1. For best practice, the client who is firing should draw visual effects, sounds, and should perform hit calculation for their own weapons in order to prevent latency from causing shots to be wrongfully missed or causing a “bad feeling” in general.
  2. Damage should be handled on the server.
  3. Replication of sounds and effects (especially visual part beams) are better handled on each client’s instead of making it on the server and performing replication automatically. To do this, you’d simply send an origin and target (and whatever other information) to each client and have them draw the beam in a way similar to #1.
  4. This is a side note, but IMHO it would be much better to use normal raycasting in something like a gun system as you would be only drawing one beam anyways with such a small projectile, and so the overhead.

I do not know how expansive and modular you are designing your weapon system to be, but IIRC Knit helps with replication automatically through pseudo-services and so you should probably use that. I have no experience with Knit, so you’re on your own there.

And for security concerns, those are always a major issue with raycast based weapons. Your best bet would be to either do serverside validation or attempt to interpolate player positions so that you can do raycasting on the server that doesn’t feel terrible.

4 Likes

Thank you, I appreciate the help. I left one MAJOR part out, the weapons are melee weapons, not any projectile-based weapons. Sorry for the complication that was my bad.

So my plan is when a player gets added, the server will go through their data and add all the Melee weapons to the Inventory UI, once that’s done I will check if they activated the melee (on client) and fire a remote event to the server to apply damage. The sounds effects and animation will be handled on the single client, possibly even sending it to the server then back to all clients. That seems to be what I can think of right now. Hopefully, this is the right way to go about it. Again I appreciate the help and you have given me a better understanding. :smiley: