Hi! So the script I'm linking below doesn't work. t's meant to undetain a player when they unequip a tool except it doesn't work. No errors

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local targetplayer = script.Parent.Parent.PlayerGui:WaitForChild("ArrestDetails",5).Frame.TextLabel.name.Value

mouse.Button1Down:Connect(function()
	if mouse.Target.Parent:FindFirstChild("Humanoid") then
		if game.Workspace[player.Name]:FindFirstChild("Arrest") then
			local targettedplayer = mouse.Target.Parent.Name
			print(targettedplayer)
			player.PlayerGui.ArrestDetails.Frame.TextLabel.Text = "Target Player: "..targettedplayer
			player.PlayerGui.ArrestDetails.Frame.TextLabel.name.Value = targettedplayer
			local userId = player.UserId
			local thumbType = Enum.ThumbnailType.HeadShot
			local thumbSize = Enum.ThumbnailSize.Size420x420
			local content, isReady = players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
			local imageLabel = player.PlayerGui.ArrestDetails.Frame.Character
			imageLabel.Image = content
			player.PlayerGui.ArrestDetails.Frame.MainGroupRank.Text = "Main Group Rank: "..game.Players[targettedplayer]:GetRoleInGroup(5552081)
			game.ReplicatedStorage.ArrestedAnimation:FireServer(targettedplayer)
			player.PlayerGui.ArrestDetails.Frame.Visible = true
			
			if mouse.Target~=nil then
			print(mouse.Target:GetFullName())
			p=nil
			_,p=pcall(function() return game.Players[mouse.Target.Parent.Name] end)
			print(p)
			print(mouse.Target.Parent.Name)
			if p~=nil then
			local detained=p
			coroutine.wrap(function()
        		local c=p
       		 while p==c do wait() pcall(function()
                p.Character.Torso.Anchored,p.Character.Torso.CFrame=false,game.Players.LocalPlayer.Character.Torso.CFrame*CFrame.new(0,0,-2.5)
				game.ReplicatedStorage.ok.Event():Connect(function(c,p)
					print("undetain event fired")
					p = nil
					c = nil
				end)
        end) end
       		 print('END OF DETAIN LOOP')
        	pcall(function() c.Character.Torso.Anchored=false end)
			
			
end)()
else print('no p') end
end
	




      


end
	end
	end)
        


		






script.Parent.Arrest.Unequipped:Connect(function()
	print("uneqipped")
	script.Parent.Parent.PlayerGui.ArrestDetails.Frame.Visible = false
				local targetplayer = script.Parent.Parent.PlayerGui.ArrestDetails.Frame.TextLabel.name.Value
				game.ReplicatedStorage.ArrestedAnimationOff:FireServer(targetplayer)
	
		
		
			
       		
                game.Players[targetplayer].Character.Torso.Anchored,game.Players[targetplayer].Character.Torso.CFrame=true,game.Players.LocalPlayer.Character.Torso.CFrame*CFrame.new(0,0,-2.5)
				game.Players[targetplayer].Character.Torso.CFrame = game.Players.LocalPlayer.Character.Torso.CFrame*CFrame.new(0,0,-2.5)
				game.Players[targetplayer].Character.Torso.Anchored = true
				game.ReplicatedStorage.ok:Fire()



end)