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!
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”:
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.