Help With Grapple System

So im creating a grapple system and basically what is happening is everytime i click a part or something like that I just go through it and get stuck so thats my issue and I dont know what could be causing this so any help is appreciated here is the code

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Rope = Instance.new("RopeConstraint")
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local target = nil
local isholdingdown = false
local isapplyingforce = false
local hit = false

function createRope(target, mousepos, isholdingdown)
	if isholdingdown == false then
		Rope:Destroy()
		return
	elseif (char.HumanoidRootPart.Position - mousepos).Magnitude > 1000 then
		return
	elseif isholdingdown == true then
		local A0 = Instance.new('Attachment')
		local A1 = Instance.new('Attachment')
		A0.Parent = workspace.Terrain
		A0.WorldPosition = mousepos
		A1.Parent = player.Character:WaitForChild("HumanoidRootPart")
		Rope = Instance.new("RopeConstraint")
		Rope.Attachment0 = A0
		Rope.Attachment1 = A1
		Rope.Parent = player.Character:WaitForChild("HumanoidRootPart")
		Rope.Visible = true
		Rope.Enabled = true
		Rope.Thickness = 0.1
		Rope.Color = BrickColor.new("Institutional white")
		Rope.Length = (player.Character:WaitForChild("HumanoidRootPart").Position - target.Position).Magnitude

		local VF = Instance.new("VectorForce");
		VF.Name = "SpiderForce";
		VF.Attachment0 = player.Character.PrimaryPart.RootRigAttachment;
		VF.Parent = player.Character.PrimaryPart;

		-- Detect using Raycasting
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {char}

		RunService.Stepped:Connect(function(_, _)
			local ray = workspace:Raycast(char.HumanoidRootPart.Position, Vector3.new(0, -3, 0) * (char.Humanoid.HipHeight + 1), params)

			if ray then
				VF.Force = Vector3.new(0, 0, 0)
				isapplyingforce = false
			else
				if isapplyingforce == false then
					if isholdingdown == true then
						VF.Force = Vector3.new(-1000, -2000, -3000)
						isapplyingforce = true
					elseif isholdingdown == false then
						return
					end
				end
			end

			-- Collision detection and rope length adjustment
			local humanoid = char:FindFirstChildOfClass("Humanoid")
			if humanoid then
				local humanoidRootPart = humanoid.RootPart
				if humanoidRootPart then
					local collisionPos = humanoidRootPart.Position
					local raycastParams = RaycastParams.new()
					raycastParams.FilterDescendantsInstances = {char, target}

					local raycastResult = workspace:Raycast(collisionPos, target.Position - collisionPos, raycastParams)
					if raycastResult then
						Rope.Length = raycastResult.Distance
					else
						Rope.Length = (player.Character:WaitForChild("HumanoidRootPart").Position - target.Position).Magnitude
					end
				end
			end

			-- Update character position based on the rope's position
			char:SetPrimaryPartCFrame(CFrame.new(target.Position))
		end)
	end
end

function InputStart(input)
	if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == true then
		if isholdingdown == false then
			isholdingdown = true
			createRope(target, mouse.Hit.Position, isholdingdown)
		end
	end
end

function InputEnd(input)
	if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == false then
		if isholdingdown == true then
			isholdingdown = false
			createRope(target, mouse.Hit.Position, isholdingdown)
		end
	end
end

mouse.Move:Connect(function()
	target = mouse.Target
	if not target then
		mouse.Icon = 'http://www.roblox.com/asset/?id=591711615'
	else
		if target:IsA("BasePart") or target:IsA("Model") then
			if target.Name == 'Baseplate' then
				return
			elseif target.Name ~= 'Baseplate' then
				UIS.InputBegan:Connect(function(input)
					InputStart()
				end)
				UIS.InputEnded:Connect(function(input)
					InputEnd()
				end)
			end
		else
			mouse.Icon = 'http://www.roblox.com/asset/?id=591711615'
		end
	end
end)

3 Likes