Best way to code weapons so it can be used by both players and npcs without any code changing?

I want to basically drag and drop weapons into players and characters and let them use it without having to basically code the client stuff for npcs on the server. The AI Handler will already have bindable events tied to activating the weapon, but I was wondering what is the best way to code weapons allowing both players and npcs to use?

3 Likes

Bumping pls help man!!!

1 Like

A way you could do it is write 2 modules: one for players, other one for NPC’s, and add an if check to the weapon script:

-- assuming its in player model
if game:GetPlayerFromCharacter(script.Parent) then -- or anything else, maybe add an attribute for all npc's
-- ... 
else
-- ...
end
2 Likes

You can also put all the important variables at the top (tool, bindeable events, etc.) and all the differences at the player-NPC check.

For example, things like UI and input should be handled by the client, and the NPC can have things like cooldowns and different movement to replicate player interaction.

I had an idea for my game about how NPCs spawn together across the map (like pillager patrols in Minecraft) and can drop weapons once killed, which can be used by the player.

1 Like