Incorrect part positioning when holstering

Recently, I created a holster system and I’ve ran into one problem so far with the system. Whenever the player is tilted or sideways the part will move inside or move away from the character’s torso.

Normal view (Usually whenever the character is looking forward or walking straight)
image

Whenever you move sideways or just walk in any direction it will go in that direction but not stay on the back
image

Better example (GIF)
cff3c27bea22eb8d6e20a55caf252908

The code for the Holster System.

--> Services
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--> References

--> Tables
local ShealthController = {}

--> Configuration
local Enabled = true -- If false (broken or disabled) will not run.

--------------------------------

function ShealthController:HolsterWeapon(Character: Model, Sword: Tool)
	if not Enabled then return warn(`[ReplicatedStorage/Modules/Shared/ShealthController]: ShealthController is currently disabled. (Did you mean to disable this?)`) end
	if Character:FindFirstChild("HolsterGroup") then
		Character:FindFirstChild("HolsterGroup"):Destroy()
	end
	
	local HolsterGroup = Instance.new("Folder")
	HolsterGroup.Name = "HolsterGroup"
	HolsterGroup.Parent = Character

	local HolsterProp = ReplicatedStorage.Assets.Holster:FindFirstChild(Sword.Name):Clone()
	HolsterProp.Name = "HolsterProp"
	if HolsterProp.Transparency == 1 then HolsterProp.Transparency = 0 end
	if Character:FindFirstChild("ArmorGroup") then
		HolsterProp.CFrame = Character:WaitForChild("Torso").CFrame * CFrame.Angles(math.rad(90),math.rad(-40),0) - Vector3.zAxis*1
	else
		HolsterProp.CFrame = Character:WaitForChild("Torso").CFrame * CFrame.Angles(math.rad(90),math.rad(-40),0) - Vector3.zAxis*0.8
	end
	HolsterProp.Parent = HolsterGroup
	
	local Weld = Instance.new("Weld")
	Weld.Name = "HolsterWeld"
	Weld.Part0 = HolsterProp
	Weld.Part1 = Character:WaitForChild("Torso")
	Weld.C1 = Character:WaitForChild("Torso").CFrame:Inverse() * HolsterProp.CFrame
	Weld.Parent = HolsterProp
end

function ShealthController:UnholsterWeapon(Character: Model)
	if not Enabled then return warn(`[ReplicatedStorage/Modules/Shared/ShealthController]: ShealthController is currently disabled. (Did you mean to disable this?)`) end
	
	if Character:FindFirstChild("HolsterGroup") then
		Character:FindFirstChild("HolsterGroup"):Destroy()
	end
end

return ShealthController

Any help would be appreciated thank you!

The issue is with how you offset the holsterprop.

You are subtracting a Vector3 from a CFrame. Doing this offsets the CFrame in world space (i.e. it does not care about rotation). You want it to be offset in local space (i.e. it does care about rotation). To do this, you have to multiply with another CFrame, instead of subtracting a Vector3.

Change

HolsterProp.CFrame = ... - Vector3.zAxis*1

To

HolsterProp.CFrame = ... * CFrame.new(0, 0, 1)

And the same for the other line where you do this.

I hope this helps!

Edit:
It may also be worth noting, the order you offset and rotate the CFrame matters, so you may want to swap the order of the CFrame.Angles and the offset.

1 Like

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