Issues with long code

Hello! Sorry for the big mess of code here. But I can’t cancel this code when released bool value is changed. I know there is a lot on this but my code can’t seem to work with any of that. So if you can help as to why the weld constraint in the character won’t break, please tell me.

local timeNeeded = 5

local captured = false
local released = false
local plrGrabbedEffect

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:WaitForChild("Humanoid", 1) then
		if hit.Parent:WaitForChild("Infected", 1) then
			if hit.Parent.Infected.Value == false then
				if hit.Parent:WaitForChild("ActivelyGrabbed").Value == false then
				if hit.Parent:WaitForChild("Torso", 1) then
						if captured == false then
							plrGrabbedEffect = hit.Parent.Name
							
							local UserPlr = game.Players:WaitForChild(script.Name)
						
						hit.Parent.ActivelyGrabbed.Value = true
						
						captured = true
						
						if game.Players:FindFirstChild(hit.Parent.Name) then
							game.ReplicatedStorage.Controls:FireClient(game.Players:FindFirstChild(hit.Parent.Name), false)
							end
					
					local plrName = script.Name
						local char = game.Workspace:FindFirstChild(plrName)
						
						game.ReplicatedStorage.Controls:FireClient(game.Players:FindFirstChild(char.Name), false)
							
							local load = char:WaitForChild("Humanoid",1):LoadAnimation(char:WaitForChild("Grab"))
							load:Stop()
							
						local load1 = char:WaitForChild("Humanoid", 1):LoadAnimation(char:WaitForChild("GrabHold"))
							
							task.wait(0.2)
							
						load1:Play()
						
						print(hit.Parent.Name.." has been grabbed by ".. script.Name)
						
						local load = hit.Parent:WaitForChild("Humanoid", 1):LoadAnimation(hit.Parent:WaitForChild("Struggle"))
						load:Play()
						
						if script.Parent then
							hit.Parent.Torso.CFrame = script.Parent.CFrame + Vector3.new(0,0.6,0)
						end
						
						for _, part in pairs(hit.Parent:GetChildren()) do
							if part:IsA("BasePart") then
								part.Position += Vector3.new(0,0.4,0)
							end
						end
						
						local weldCon = Instance.new("WeldConstraint")
						weldCon.Parent = char
						weldCon.Part0 = script.Parent
							weldCon.Part1 = hit.Parent:WaitForChild("HumanoidRootPart")
							weldCon.Name = "WeldCon"
						
							task.wait(timeNeeded)
							
							if released == false then
							
							hit.Parent:WaitForChild("ActivelyGrabbed").Value = false

						hit.Parent.Infected.Value = true
						
						game.ReplicatedStorage.Controls:FireClient(game.Players:FindFirstChild(char.Name), true)
						if game.Players:FindFirstChild(hit.Parent.Name) then
							game.ReplicatedStorage.Controls:FireClient(game.Players:FindFirstChild(hit.Parent.Name), true)
							end
							for _, part in pairs(hit.Parent:GetChildren()) do
								if part:IsA("BasePart") or part:IsA("MeshPart") then
									part.BrickColor = BrickColor.new("Moss")
								end
								if part:IsA("Accessory") or part:IsA("Pants") or part:IsA("Shirt") then
									part:Destroy()
								end
								if part.Name == "Torso" or part.Name == "Right Leg" or part.Name == "Left Leg" then
									part.BrickColor = BrickColor.new("Dirt brown")
								end
								end			
								end
						
						
							if script.Parent then
								UserPlr.Character.IsGrabbing.Value = false
							weldCon:Destroy()
						script.Parent:Destroy()
							end

						end
						end
					end
			end
		end
			end
end)

task.wait(1.25)
if captured == false then
	if script.Parent then
		local UserPlr = game.Players:WaitForChild(script.Name)
		UserPlr.Character.IsGrabbing.Value = false
		script.Parent:Destroy()
	end
end

game.ReplicatedStorage.ReleaseGrab.OnServerEvent:Connect(function()
	released = true
	local player = script.Name
	local character = game.Workspace:FindFirstChild(player)
		character:WaitForChild("WeldCon"):Destroy()
	if script.Parent and script then
		script.Parent:Destroy()
		script:Destroy()
	end
	game.ReplicatedStorage.Controls:FireClient(script.Name, true)
	game.ReplicatedStorage.Controls:FireClient(plrGrabbedEffect, true)
	
	script.Disabled = true
end)

Can you explain the general purpose and if it does an error?

1 Like

No errors, though the purpose is a grab code.