Source code:
--Grapple hook script that utelizes the mouse to create a rope that will use a winch to act like a Hook.
local Player = game.Players.LocalPlayer
local HumanoidRootPart = Player.Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()
local Debounce = false
local mouseDown = false
local Max_Distance = 300 -- This is the max length that the grapple hook can get to
local WinchSpeed = 500 --Winch Speed.
local RopeVisible = true --Is the rope visible?
local RopeColor = BrickColor.new("Institutional white")
-- Calculate the distance between two Vector3 objects and round to the nearest ten
local function distance_between_vectors(u, v)
-- Calculate the difference between the two vectors
local difference = u - v
-- Calculate the magnitude (distance) of the difference vector
local distance = difference.Magnitude
-- Round the distance to the nearest ten
distance = math.floor(distance / 1 + 0.5) * 1
-- Return the rounded distance
return distance
end
local function Attachment(method, parent)
if method == "Create" then
local Attachement = Instance.new("Attachment")
Attachement.Parent = parent
else
parent.Attachment:Destroy()
end
end
Mouse.Button1Up:Connect(function()
mouseDown = false
end)
Mouse.Button1Down:Connect(function()
mouseDown = true
if Debounce == false then
Debounce = true
local HitPosition = Mouse.Hit.p
local distance = distance_between_vectors(HitPosition, HumanoidRootPart.Position) --Fires our function above passing the two vectors
if distance <= Max_Distance then --If distance is smaller then Max_Difference run the code below
local NewPart = Instance.new("Part") --Creates a new part
NewPart.Anchored = true --Makes the part anchored
NewPart.Parent = game.Workspace --Moves it to the workspace
NewPart.Position = HitPosition --Moves the part to the position
NewPart.Transparency = 1
Attachment("Create", NewPart)
Attachment("Create", HumanoidRootPart)
local Rope = Instance.new("RopeConstraint")
Rope.Parent = NewPart
Rope.Attachment0 = NewPart.Attachment
Rope.Attachment1 = HumanoidRootPart.Attachment
Rope.WinchEnabled = true
Rope.WinchTarget = 5
Rope.WinchForce = 1000000000
Rope.Restitution = 0
Rope.Color = RopeColor
Rope.Visible = RopeVisible
Rope.Length = distance / 2
Rope.WinchSpeed = WinchSpeed
while mouseDown == true do
wait()
end
Attachment("Destroy", NewPart)
Attachment("Destroy", HumanoidRootPart)
NewPart.RopeConstraint:Destroy()
NewPart:Destroy()
end
Debounce = false
end
end)