Weld:Destroy() not working

I’m working on a handcuff system, where basically I want a Weld to be destroyed when a UI button is pressed, but this doesn’t work (for some reason it only works the first time you use it?). I’ve already tried a repeat loop, but it keeps looping forever.

ServerScript (StarterPack/CuffTool):

function ReleasePlayer(Player, CuffedPlr)
	local Char = CuffedPlr.Character
	local SuspectValue = Player.PlayerGui.CuffGUI:WaitForChild("DetainedSuspect")
	local Weld = Char.HumanoidRootPart:WaitForChild("DetainWeld")
	Weld:Destroy()
	if Char.HumanoidRootPart:FindFirstChild("DetainWeld") then
		repeat task.wait(0.5) Weld:Destroy() print("Destroyinh") until Char.HumanoidRootPart:FindFirstChild("DetainWeld") == nil
	else
		Weld = nil
	end
	SuspectValue:Destroy()
	local Humanoid = Char:WaitForChild("Humanoid")
	local Anmations = Humanoid:GetPlayingAnimationTracks()
	for i, Anim in pairs(Anmations) do
		if Anim.Name == "ArrestPlayer" then
			Anim:Stop()
		end
	end
	for i, v in pairs(Char:GetChildren()) do
		if v:IsA("BasePart") then
			v.Massless = false
		end
	end
	for i, Tool in pairs(CuffedPlr.ToolsNotInUse:GetChildren()) do
		Tool.Parent = CuffedPlr.Backpack
	end
	Humanoid.PlatformStand = false
	Humanoid.WalkSpeed = 16
	InUse = false
	ResetButtonRemote:FireClient(CuffedPlr,"Enable")
	Char:FindFirstChild("GrabPrompt").Enabled = false
end
1 Like

Could you show us the code that handcuffs the player?

2 Likes

Sure thing, there are 2 versions 1 for whent he tool is used and 1 when a prompt is used.

Tool:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Config = require(workspace.CuffSystem.Config)
local Players = game:GetService("Players")
local RemoteEvent = ReplicatedStorage.ExploitProtectionRemoteEvents.RemoteEvent
local Remote2 = ReplicatedStorage.ExploitProtectionRemoteEvents.CuffedPlayer
local ReleaseRemote = ReplicatedStorage.ExploitProtectionRemoteEvents.ReleasePlayer
local DetainRemote = ReplicatedStorage.ExploitProtectionRemoteEvents.DetainPlayer
local ResetButtonRemote = ReplicatedStorage.ExploitProtectionRemoteEvents.DisableResetButton
local InUse = false

function PlayerCuffed(Player, Target)
	local IsPlayer = Players:GetPlayerFromCharacter(Target.Parent)
	local PlayerHasTool = Player.Character:FindFirstChild("Cuffs")
	if Config.CopsOnly then
		local Check = false
		for i, Color in pairs(Config.PoliceTeams) do
			if Color == Player.TeamColor then
				Check = true
				break
			end
		end
		if not Check then
			return
		end
	end
	if not InUse and PlayerHasTool then
		if Target.Name == "Torso" or Target.Name == "HumanoidRootPart" and IsPlayer ~= nil then
			local CheckDistance = (Player.Character.HumanoidRootPart.Position - Target.Position).Magnitude
			if CheckDistance <= 15 then
				local Char = Target.Parent
				local Humanoid = Char:FindFirstChild("Humanoid")
				local WeldCheck = Char.HumanoidRootPart:FindFirstChild("DetainWeld")
				if Humanoid  and WeldCheck == nil then
					Humanoid.PlatformStand = true
					local Anim = script.Parent:WaitForChild("ArrestPlayer")
					local AnimationTrack = Humanoid:LoadAnimation(Anim)
					AnimationTrack:Play()
					task.wait(0.1)
					for i, v in pairs(Char:GetChildren()) do
						if v:IsA("BasePart") then
							v.Massless = true
						end
					end
					local Weld = Instance.new("Weld")
					Weld.Name = "DetainWeld"
					Weld.Parent = Char.HumanoidRootPart
					Weld.Part0 = Player.Character.HumanoidRootPart
					Weld.Part1 = Char.HumanoidRootPart
					Weld.C1 = CFrame.new(0,0,3.5)
					local Suspect = Instance.new("StringValue")
					Suspect.Parent = Player.PlayerGui.CuffGUI
					Suspect.Name = "DetainedSuspect"
					Suspect.Value = IsPlayer.Name
					Char.Humanoid.WalkSpeed = 0
					Remote2:FireClient(Player,IsPlayer)
					ResetButtonRemote:FireClient(IsPlayer,"Disable")
					InUse = true
					for i, Tool in pairs(IsPlayer.Backpack:GetChildren()) do
						Tool.Parent = IsPlayer.ToolsNotInUse
					end
				end
			end
		end
	elseif not PlayerHasTool then
		Player:Kick("No exploiting please! - Jan's anti exploit.")
	end
end

Prompt:

local RP = game:GetService("ReplicatedStorage")
local CuffedPlr = game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local ResetButtonRemote = RP.ExploitProtectionRemoteEvents:FindFirstChild("DisableResetButton")
local Remote2 = RP.ExploitProtectionRemoteEvents.CuffedPlayer
local Char = script.Parent.Parent

local function OnTriggered(Player)
	local PlayerHasTool = Player.Character:FindFirstChild("Cuffs")
	local PlayerHasTool2 = Player.Backpack:FindFirstChild("Cuffs")
	if PlayerHasTool or PlayerHasTool2 then
		local Humanoid = Char:FindFirstChild("Humanoid")
		local WeldCheck = Char.HumanoidRootPart:FindFirstChild("DetainWeld")
		if Humanoid and WeldCheck == nil then
			Humanoid.PlatformStand = true
			local Anim = script.Parent:WaitForChild("ArrestPlayer")
			local AnimationTrack = Humanoid:LoadAnimation(Anim)
			AnimationTrack:Play()
			task.wait(0.1)
			for i, v in pairs(Char:GetChildren()) do
				if v:IsA("BasePart") then
					v.Massless = true
				end
			end
			local Weld = Instance.new("Weld")
			Weld.Name = "DetainWeld"
			Weld.Parent = Char.HumanoidRootPart
			Weld.Part0 = Player.Character.HumanoidRootPart
			Weld.Part1 = Char.HumanoidRootPart
			Weld.C1 = CFrame.new(0,0,3.5)
			local Suspect = Instance.new("StringValue")
			Suspect.Parent = Player.PlayerGui.CuffGUI
			Suspect.Name = "DetainedSuspect"
			Suspect.Value = Player.Name
			Char.Humanoid.WalkSpeed = 0
			Remote2:FireClient(Player,CuffedPlr)
			ResetButtonRemote:FireClient(Player,"Disable")
			for i, Tool in pairs(Player.Backpack:GetChildren()) do
				Tool.Parent = Player.ToolsNotInUse
			end
			script.Parent.Enabled = false
		end
	else
		return
	end
end



script.Parent.Triggered:Connect(OnTriggered)
1 Like

I think that the problem has something to do with this repeat in the first script you showed:

repeat task.wait(0.5) Weld:Destroy() print("Destroyinh") until Char.HumanoidRootPart:FindFirstChild("DetainWeld") == nil

Instead of a repeat, maybe you could try looping through all the objects in the HumanoidRootPart and then destroy all instances that are Weld instances and have the name “DetainWeld”:

for _, Object in ipairs(Char.HumanoidRootPart:GetChildren()) do
	if Object:IsA("Weld") == false or Object.Name ~= "DetainWeld" then continue end
	Object:Destroy()
end
3 Likes

It doesn’t work the Weld just stays. It’s so weird.

2 Likes

Maybe the problem has to do with the fact you’re creating the welds on the client, maybe you should fire a remote that causes the weld to be created, so it exists on the server.

1 Like

The welds are created on the server. Sorry if that was unclear.

Nvm I made a mistake, your solution works, Thanks! (This stuff was driving me crazy)

1 Like