I want to make it so if someone has a tool it will give them a holster to keep it in, then once they take it out the holster has a the tool in it, so it will disappear then when they don’t have it open it will come back in on the holster
It will do the thing where it goes in and out of the holster, and it goes in the holster, but say I remove the tool from my inventory, the holster stays, say if I didn’t have the tool but then I receive it, it doesn’t give the holster then, you have to spawn in with it or without it, I don’t want that.
I’ve tried separating the scripts and re-formatting it, it hasn’t been working so far
local player = script:FindFirstAncestorWhichIsA("Player") or game:GetService"Players":GetPlayerFromCharacter(script.Parent.Parent.Parent)
local g = script.Parent.Parent.Holster:clone()
while true do
wait()
if player.Backpack:WaitForChild("USP") or player.Character:WaitForChild("USP") then
g.Parent = player
local C = g:GetChildren()
for i=1, #C do
if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
local W = Instance.new("Weld")
W.Part0 = script.Parent
W.Part1 = C[i]
local CJ = CFrame.new(script.Parent.Position)
local C0 = script.Parent.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = script.Parent
end
local Y = Instance.new("Weld")
Y.Part0 = player.Character["Right Leg"]
Y.Part1 = script.Parent
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end
local h = g:GetChildren()
for i = 1, # h do
if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
h[i].Anchored = false
h[i].CanCollide = false
end
end
end
if player.Backpack:FindFirstChild("USP") == nil then
g:Destroy()
end
if player.Backpack:WaitForChild("USP") then
script.Parent.Parent.Holster.Bolt.Transparency = 0
script.Parent.Parent.Holster.Handle.Transparency = 0
script.Parent.Parent.Holster.Mag.Transparency = 0
script.Parent.Parent.Holster.Grip.Transparency = 0
end
if player.Character:WaitForChild("USP") then
script.Parent.Parent.Holster.Bolt.Transparency = 1
script.Parent.Parent.Holster.Handle.Transparency = 1
script.Parent.Parent.Holster.Mag.Transparency = 1
script.Parent.Parent.Holster.Grip.Transparency = 1
end
end
Why use a holster loop, when you can do it like this:
Put server script inside the tool.
local tool = script.Parent
local PS = game:GetService("Players")
tool.Equipped:Connect(function()
if PS:GetPlayerFromCharacter(script.Parent) then
local g = tool.Holster:Clone()
local plr = PS:GetPlayerFromCharacter(script.Parent)
local char = plr.Character
g.Parent = char
g.Position = char.HumanoidRootPart.Position
local W = Instance.new("Weld")
W.Parent = g
W.Part0 = g
W.Part1 = char.HumanoidRootPart
g.Anchored = false
g.CFrame = CFrame.new(0,0,0)-- Edit this X,Y,Z
end
end)
tool.Unequipped:Connect(function()
if PS:GetPlayerFromCharacter(script.Parent.Parent) then
local plr = PS:GetPlayerFromCharacter(script.Parent.Parent)
local char = plr.Character
if char:FindFirstChild("Holster") then
char.Holster:Destroy()
end
end
end)
local Tool = script.Parent
function onUnequipped()
if script.Parent.Parent == workspace or script.Parent.Parent.className ~= "Backpack" then
return
end
local char = script.Parent.Parent.Parent.Character
if char ~= nil then
if char:FindFirstChild("BackWeld") then
char.BackWeld:Destroy()
end
local torso = char:FindFirstChild("Torso")
local tool = char:FindFirstChild(script.Parent.Name)
if torso ~= nil and tool == nil then
local model = Instance.new("Model")
model.Name = "BackWeld"
model.Parent = char
local handle = script.Parent.Handle:Clone()
handle.Name = "WeldHandle"
handle.CanCollide = false
handle.Parent = model
local weld = Instance.new("Weld")
weld.Name = "BackWeld"
weld.Part0 = torso
weld.Part1 = handle
weld.C0 = CFrame.new(0,0,0.6)
weld.C0 = weld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),-110.5) --You need to edit this line
weld.Parent = handle
end
end
end
function onEquipped()
local char = script.Parent.Parent
if char:FindFirstChild("BackWeld") then
char.BackWeld:Destroy()
end
end
Tool.Unequipped:Connect(onUnequipped)
Tool.Equipped:Connect(onEquipped)
If interested, I made a tutorial for creating a basic item holstering system in January of 2021 which utilizes Accessories to simulate holstered tools when an item has been unequipped.
Using Accessories makes it possible to avoid needing to weld parts from the Accessory to the Character in real-time since attachments can be used to pre-position it as intended while editing the game. However, If the Tool that you want to turn into an Accessory has multiple parts, make sure that those parts have been welded together beforehand.
local Tool = script.Parent
function onUnequipped()
if script.Parent.Parent == workspace or script.Parent.Parent.className ~= "Backpack" then
return
end
local char = script.Parent.Parent.Parent.Character
if char ~= nil then
if char:FindFirstChild("WeldHandle") then
char.WeldHandle:Destroy()
end
local torso = char:FindFirstChild("Torso")
local tool = char:FindFirstChild(script.Parent.Name)
if torso ~= nil and tool == nil then
local handle = Tool.Handle:Clone()
handle.Parent = char
handle.Name = "WeldHandle"
local weld = Instance.new("Weld")
weld.Name = "WeldHolster"
weld.Part0 = torso
weld.Part1 = handle
weld.C0 = CFrame.new(0,0,0.6)
weld.C0 = weld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),-110.5) --You need to edit this line
weld.Parent = handle
end
end
end
function onEquipped()
local char = script.Parent.Parent
if char:FindFirstChild("WeldHandle") then
char.WeldHandle:Destroy()
end
end
Tool.Unequipped:Connect(onUnequipped)
Tool.Equipped:Connect(onEquipped)
function onEquipped()
local char = script.Parent.Parent
if char:FindFirstChild("WeldHandle") then
char.WeldHandle.Thing:Destroy()
char.WeldHandle.Thing:Destroy()
char.WeldHandle.Thing:Destroy()
char.WeldHandle.Thing:Destroy()
end
end
Tool.Unequipped:Connect(onUnequipped)
function onUnequipped()
if script.Parent.Parent == workspace or script.Parent.Parent.className ~= "Backpack" then
return
end
local char = script.Parent.Parent.Parent.Character
if char ~= nil then
-- Delete this line
------------------------------------------------------------------------------
if char:FindFirstChild("WeldHandle") then
char.WeldHandle:Destroy()
end
------------------------------------------------------------------------------
local torso = char:FindFirstChild("Torso")
local tool = char:FindFirstChild(script.Parent.Name)
if torso ~= nil and tool == nil then
local handle = Tool.Handle:Clone()
handle.Parent = char
handle.Name = "WeldHandle"
local weld = Instance.new("Weld")
weld.Name = "WeldHolster"
weld.Part0 = torso
weld.Part1 = handle
weld.C0 = CFrame.new(0,0,0.6)
weld.C0 = weld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),-110.5) --You need to edit this line
weld.Parent = handle
end
end
end
local Tool = script.Parent
local Handle = Tool.Handle
local function onUnequipped()
local Player = script:FindFirstAncestorOfClass("Player")
if Player then
local Character = Player.Character
local WeldHandle = Character:FindFirstChild("WeldHandle")
if WeldHandle then
WeldHandle:Destroy()
end
local Torso = Character:FindFirstChild("Torso")
local HandleClone = Handle:Clone()
HandleClone.Parent = Character
HandleClone.Name = "WeldHandle"
local Weld = Instance.new("Weld")
Weld.Name = "WeldHolster"
Weld.Part0 = Torso
Weld.Part1 = HandleClone
Weld.C0 = CFrame.new(0, 0, 1)
Weld.C0 = Weld.C0 * CFrame.Angles(0, 0, math.rad(-110))
Weld.Parent = HandleClone
end
end
local function onEquipped()
local Character = Tool.Parent
local WeldHandle = Character:FindFirstChild("WeldHandle")
if WeldHandle then
WeldHandle.WeldHolster:Destroy()
end
end
Tool.Unequipped:Connect(onUnequipped)
Tool.Equipped:Connect(onEquipped)