Having trouble with holster

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 done

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

Just use

tool.Equipped:Connect(function()

end)

and

tool.Unequipped:Connect(function()

end)

You only need to change/add 1 weld which is connected to the primary part.

Oh yeah this wont work:

script:FindFirstAncestorWhichIsA("Player")

As player.Character is a property not a child and the character is parented to workspace.

so what do I need to do? sorry confused

If this is a server script then that last line of code is valid. I’d opt to use “FindFirstAncestorOfClass()” instead as it’s more precise, try the following code in a server script placed inside a tool.

local Player = script:FindFirstAncestorOfClass("Player")
print(Player.Name)

ok this worked, but it only works when the tool is equipped

repro.rbxm (1.1 KB)

Works without the tool needing to be equipped.

ok let me try it out thank you

no Like I put the leg holster in the tool with the script and now it only comes up when it is equipped