Place Swords and Guns in hand without the use of a tool and in one line

Good day to whoever is reading this,

When I first started coding on ROBLOX I remember that I really wanted to make an RPG game, but always despised the default tools ROBLOX offered. Although now a much better coder, I feel that I can’t be the only one who struggled with placing items on a player’s hand, most notably swords.

Long story short, I wrote a module script which does it for you in one line.

local Hand_ler = {}

-- Requiring service because this is where I store my weapons
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Table to determine where to place weapon
local hand_table = {"Right","Left","Both"}

-- Modify this line to locate where your swords are. Should be in a folder for organization purposes
local weapons_folder = ReplicatedStorage.Swords

-- Makes sure nothing is invalid
local function CheckForMisinputs(character: Model, weapon: string, hand: string)
	if not character then warn("No Character") return false end
	if not weapon then warn("No weapon name recieved") return false end
	if not table.find(hand_table,hand) then warn("Invalid hand input, please select from Right, Left and Both") return false end
	return true
end


-- Looks for a part only found in R15 characters then returns the correct grip
local function GripFinder(character: Model, hand: string)
	if character:FindFirstChild("UpperTorso") then
		return character[hand.."Hand"][hand.."GripAttachment"]
	else
		return character[hand.." Arm"][hand.."GripAttachment"]
	end
end

-- Cleanup Crew for weapon swapping
local function DestroyLingeringWeapons(character: Model)
	for _,item in character:GetChildren() do
		if item:HasTag("Weapon") then
			item:Destroy()
		end
	end
end

-- Coded in in case people forgot to weld the sword meshes/parts together

local function Welder(sword: Model)
	if not sword:FindFirstChild("Handle") then warn("No Handle detected by the welder script") return false end
	local root = sword.Handle
	local weld_folder = Instance.new("Folder",root)
	
	for _,child in sword:GetChildren() do
		if child ~= root then
			local weld = Instance.new("WeldConstraint",weld_folder)
			weld.Parent = weld_folder
			weld.Part0 = root
			weld.Part1 = child
			
			child.Anchored = false
			child.CanCollide = false
		end
	end
end

-- Applies the physical changes
local function Equip(character: Model, weapon_string: string, hand: string)
	local weapon = weapons_folder:FindFirstChild(weapon_string):Clone()
	local joint = Instance.new("RigidConstraint")
	
	if weapon:FindFirstChild("Handle") then
		if weapon.Handle:FindFirstChild("Attachment") then
			weapon.Parent = character
			weapon:AddTag("Weapon")
			
			joint.Parent = weapon
			joint.Attachment0 = GripFinder(character,hand)
			joint.Attachment1 = weapon.Handle.Attachment
		else
			weapon:Destroy()
			joint:Destroy()
			return warn("Handle doesn't have an attachment named 'Attachment'")
		end
	else
		weapon:Destroy()
		joint:Destroy()
		return warn("Sword doesn't have a handle, please add one and name is Handle")
	end
end

-- This is the function you call in your script
function Hand_ler:AppendSword(character: Model, weapon: string, hand: string)
	if CheckForMisinputs(character,weapon,hand) then
		DestroyLingeringWeapons(character)
		if hand == "Both" then
			Equip(character,weapon,"Right")
			Equip(character,weapon,"Left")
		else
			Equip(character,weapon,hand)
		end
	end
end

return Hand_ler

While the module itself is pretty straightforward, I feel that some might want an example of the code in action.

-- This has been placed in ServerScriptService 
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local WeaponHandler = require(ServerStorage.Libs.CombatAccessoryHandler)

local sword_name = "Sword"

Players.PlayerAdded:Connect(function(player: Player)
	local character = player.Character or player.CharacterAdded:Wait()
	WeaponHandler:AppendSword(character,sword_name,"Both")
end)

This is a very simple script, and I do not think I will be updating it more as it works perfectly fine for my projects. Feel free to tweak it however you see fit and make it work for you.

5 Likes