Grappling script glich - Rope won't destroy

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local canSwingTag = "CanSwing" -- Tag to identify parts that can be grappled onto
local ropeMaxDistance = 50 -- Maximum distance for the rope

-- Function to check if a part can be grappled onto
local function canSwing(part)
	return part:IsA("BasePart") and part:GetAttribute(canSwingTag) == true
end

-- Function to handle grappling
local function grapple()
	local target = mouse.Target
	if target and canSwing(target) then
		local character = player.Character
		if character then
			local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
			local distance = (target.Position - humanoidRootPart.Position).magnitude
			if distance <= ropeMaxDistance then
				-- Create rope attachment
				local rope = Instance.new("RopeConstraint")
				rope.Parent = humanoidRootPart
				rope.Attachment0 = humanoidRootPart:FindFirstChild("RopeAttachment") or Instance.new("Attachment", humanoidRootPart)
				rope.Attachment1 = target:FindFirstChild("RopeAttachment") or Instance.new("Attachment", target)
				rope.Visible = true
				rope.Length = distance
				rope.Thickness = 0.4
				rope.Color = Color3.new(0, 0, 0)
				if mouse.Button2Up then
					rope:Destroy()
				end
			else
				print("Target is too far away.")
			end
		end
	else
		print("Cannot grapple onto this part.")
	end
end

-- Listen for right mouse button click
mouse.Button2Down:Connect(function()
	grapple()
end)

how this script works :- when we click the right mouse button a rope appears which connects the HumanoidRootPart with the target.

but at line “30”. when i typed rope:Destroy( ), the rope isnt destroying
Even when i stop pressing the right mouse button the rope isnt destroying.

Can you show whats the mistake done in this script ?
Thanks!

1 Like

Welcome to the forums @mddmakes! :orangutan:


Could you please update your code using code tags so it is easier for us to read?

Im really sorry but i dont really know what code tags are… ;-;

Edit your post and press the code button in your composer (above where you type), then paste your code inside of the box that is created.

Okay, I’ve completed it!

What happens when you print the value of mouse.Button2Up?

wha-what?.. im sorry but i dont really get what ur saying … ;-;

im really sorry if im causing you trouble. ;-;

If you print mouse.Button2Up what does it say?

He means to add a print(mouse.Button2Up) and check your Output.


cut to the chase

issue with your script is most likely in the way you’re checking for mouse button release. the mouse.Button2Up event isn’t being used correctly to destroy the rope when the right mouse button is released. i put together something like this:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local canSwingTag = "CanSwing" -- Tag to identify parts that can be grappled onto
local ropeMaxDistance = 50 -- Maximum distance for the rope

-- Function to check if a part can be grappled onto
local function canSwing(part)
    return part:IsA("BasePart") and part:GetAttribute(canSwingTag) == true
end

-- Function to handle grappling
local function grapple()
    local target = mouse.Target
    if target and canSwing(target) then
        local character = player.Character
        if character then
            local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
            local distance = (target.Position - humanoidRootPart.Position).magnitude
            if distance <= ropeMaxDistance then
                -- Create rope attachment
                local rope = Instance.new("RopeConstraint")
                rope.Parent = humanoidRootPart
                rope.Attachment0 = humanoidRootPart:FindFirstChild("RopeAttachment") or Instance.new("Attachment", humanoidRootPart)
                rope.Attachment1 = target:FindFirstChild("RopeAttachment") or Instance.new("Attachment", target)
                rope.Visible = true
                rope.Length = distance
                rope.Thickness = 0.4
                rope.Color = Color3.new(0, 0, 0)
                
                -- Listen for right mouse button release
                local function destroyRope()
                    rope:Destroy()
                    mouse.Button2Up:Disconnect(destroyRope) -- Disconnect the event listener
                end
                mouse.Button2Up:Connect(destroyRope) -- Connect the event listener
            else
                print("Target is too far away.")
            end
        end
    else
        print("Cannot grapple onto this part.")
    end
end

-- Listen for right mouse button click
mouse.Button2Down:Connect(function()
    grapple()
end)

what i changed:

a new function destroyRope is created within the grapple function, which destroys the rope constraint when the right mouse button is released. this function is connected to the mouse.Button2Up event in the grapple function. so then this make sures that the rope is destroyed when the right mouse button is released.

for reference, this is the “right mouse button”:
image

future reference

next time please send a screenshot of your output as it might make it easier and it’ll be easier to debug

if this doesn’t work

try getting the path to the rope in a variable, and destroy it normally in the command bar?
it’d look something like:

local rope = --path to rope here     rope:Destroy()

when you run it, lmk what happens, specifically whether the rope gets destroyed or not, and if not, then the error.

feel free to mark me as solution if i helped lol

let me know if you have any more questions

sorry i was sleeping at that time so i couldn’t respond ;-;

Thank you so so much ! but the rope dosent delete itself after i stop pressing the right mouse button

btw this script is in “startergui”

I fixed the code myself!

What i changed--------
I created a function called “releaseGrapple” and made it activate when the right mouse button goes up

THANK YOU SO SO MUCH FOR YOUR HELP @orangebird3 and @Noah ! i appreciate it!!! :smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.