No clue what to name this

making something and basically i have it where i bind the characters arms onto the camera to basically be a “viewmodel” for the guns.

as of right now it is its own script. i have plans to move it it elsewhere but not sure which option would be better.

Option 1:
Move the code into the guns themselves, having it bind the arms on equip and unbind them on unequip.

Option 2:
Move the code into the player’s main localscript. detecting if a gun is equipped then binding the arms.

Just wondering which one of these would be the better route.

The Uninteresting code
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RunService = game:GetService("RunService")
repeat task.wait() until game:IsLoaded()
repeat task.wait() until Character:IsDescendantOf(workspace)

-- Variables --
local Binded = false
local FTorso
local DebugMovementDelay = false
local DebugMovementDelayNum = 1
local BindRenderStep
local BindCoroutine
--           --

local RightUpperArm = Character:WaitForChild("RightUpperArm")
local LeftUpperArm = Character:WaitForChild("LeftUpperArm")
local RightArmMotor = Character:WaitForChild("RightUpperArm"):WaitForChild("RightShoulder")
local LeftArmMotor = Character:WaitForChild("LeftUpperArm"):WaitForChild("LeftShoulder")
local UpperTorso = Character:WaitForChild("UpperTorso")

local function BindArms()
	if not Binded then
		Binded = true
		FTorso = Instance.new("Part")
		FTorso.Anchored = true
		FTorso.CanCollide = false
		FTorso.CanQuery = false
		FTorso.CanTouch = false
		FTorso.Transparency = .7
		FTorso.CastShadow = false

		FTorso.Size = Vector3.new(
			Character.UpperTorso.Size.X,
			math.floor((Character.UpperTorso.Size.Y + Character.LowerTorso.Size.Y)),
			Character.UpperTorso.Size.Z
		)

		FTorso.Parent = workspace.Camera
		FTorso.Name = "UpperTorso"

		RightArmMotor.Part0 = FTorso
		LeftArmMotor.Part0 = FTorso
		RightArmMotor.C0 = CFrame.new(RightArmMotor.C0.Position)-- * CFrame.Angles(math.rad(90),0,0)
		RightArmMotor.C1 = CFrame.new(RightArmMotor.C1.Position)
		LeftArmMotor.C0 = CFrame.new(LeftArmMotor.C0.Position)-- * CFrame.Angles(math.rad(90),0,0)
		LeftArmMotor.C1 = CFrame.new(LeftArmMotor.C1.Position)


		if DebugMovementDelay then
			BindCoroutine = coroutine.create(function()
				while task.wait(DebugMovementDelayNum) do
					FTorso.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0,-1.8,-.5)
					--FTorso.CFrame = CFrame.new(0,10,0)
				end
			end)
			coroutine.resume(BindCoroutine)
		else
			BindCoroutine = coroutine.create(function()
				BindRenderStep = RunService.RenderStepped:Connect(function()
					FTorso.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0,-1.8,-.5)
					--FTorso.CFrame = CFrame.new(0,10,0)
				end)
			end)
			coroutine.resume(BindCoroutine)
		end
	else
		warn("Arms already binded")
	end
end

local function UnbindArms()
	if Binded then
		if FTorso then FTorso:Destroy() end
		
		BindRenderStep:Disconnect()
		coroutine.close(BindCoroutine)
		
		RightArmMotor.Part0 = UpperTorso
		LeftArmMotor.Part0 = UpperTorso
		
		RightArmMotor.C0 = CFrame.new(RightArmMotor.C0.Position)
		RightArmMotor.C1 = CFrame.new(RightArmMotor.C1.Position)
		
		LeftArmMotor.C0 = CFrame.new(LeftArmMotor.C0.Position)
		LeftArmMotor.C1 = CFrame.new(LeftArmMotor.C1.Position)
	end
end


BindArms()

if you want flexibility than its better to make it in one script

1 Like

you could put the function into a module script and then use a script for each weapon to handle the weapon itself and bind the arms

1 Like

hm, not sure if im doing it by your means but is it like this?

Module Script

local GenericModule = {}

GenericModule.BindArms = function()
	-- Code for binding arms
end

GenericModule.UnBindArms = function()
	-- Code for unbinding arms
end

return GenericModule

LocalScript

local BindModule = require(script.GenericModule)

local function CheckToBind()
	-- Checking code here
	return true, "Bind"	
end

local function CharacterToolStatusChanged()
	local ToBind, Bind = CheckToBind()
	
	if ToBind then
		if Bind == "Bind" then
			BindModule.BindArms()
		elseif Bind == "UnBind" then
			BindModule.UnBindArms()
		end
	end
end

of course its not completed code as its something i scrapped together in like a minute to check if this is what you mean.

1 Like

yep exactly, this way you hold control over each tool. lets say you want a few to not show arms just not apply the bind in the local script of the tool.

Yet you still dont make unnecesary copies of the script. if you where to change the bind module it will apply to all weapons.

1 Like

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