Search code examples
luaroblox-studio

Grapple hooking with LineForces in Roblox Studio?


I am making a grappling hook for a game about grappling through floating islands while collecting treasure, and recently I've been playing around with constraints and forces (vector forces, line forces, spring / rope constraints, etc.,). Anyways, I tried adapting a script to use line forces instead of rope constraints, but it is not working. There are no errors, so I am very confused.

I tried using this code:

local player = game.Players.LocalPlayer
local HumanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local debounce = false
local mouseDown = false

local MaxDistance = 300
local Magnitude = 10000
local RopeVisible = true
local RopeColor = BrickColor.new("Cocoa")

local function distance_between_vectors(u, v)
    local difference = u - v
    local distance = difference.Magnitude
    
    distance = math.floor(distance / 1 + 0.5) * 1
    return distance
end

local function Attachment(method, parent)
    if method == "Create" then
        local Attachment = Instance.new("Attachment")
        Attachment.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.Position
        local distance = distance_between_vectors(hitPosition, HumanoidRootPart.Position)
        if distance <= MaxDistance then
            local newPart = Instance.new("Part")
            newPart.Anchored = true
            newPart.Parent = game.Workspace
            newPart.Position = hitPosition
            newPart.Transparency = 0
            Attachment("Create", newPart)
            Attachment("Create", HumanoidRootPart)
            local Rope = Instance.new("LineForce")
            Rope.Parent = newPart
            Rope.Attachment0 = newPart.Attachment
            Rope.Attachment1 = HumanoidRootPart.Attachment
            Rope.Magnitude = Magnitude
            Rope.Visible = RopeVisible
            Rope.Color = RopeColor
            Rope.MaxForce = 1000
            while mouseDown == true do
                wait()
            end
            Attachment("Destroy", newPart)
            Attachment("Destroy", HumanoidRootPart)
            newPart.LineForce:Destroy()
            newPart:Destroy()
        end
        debounce = false
    end
end)

This, for some reason, refuses to work. Can anyone help me?

Thank you, Programming_Good_Games


Solution

  • So, it turns out that a rod constraint was the way to go. Sorry to anyone who was going to try to help. I'll put the script so that anyone that needs it can use it:

    local player = game.Players.LocalPlayer
    local HumanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
    local mouse = player:GetMouse()
    local debounce = false
    local mouseDown = false
    
    local MaxDistance = 500
    local RopeVisible = true
    local RopeColor = BrickColor.new("Cocoa")
    
    local function distance_between_vectors(u, v)
        local difference = u - v
        local distance = difference.Magnitude
        
        distance = math.floor(distance / 1 + 0.5) * 1
        return distance
    end
    
    local function Attachment(method, parent)
        if method == "Create" then
            local Attachment = Instance.new("Attachment")
            Attachment.Parent = parent
        else
            if parent:FindFirstChild("Attachment") then
                parent.Attachment:Destroy()
            end
        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.Position
            local distance = distance_between_vectors(hitPosition, HumanoidRootPart.Position)
            if distance <= MaxDistance then
                local newPart = Instance.new("Part")
                newPart.Anchored = true
                newPart.Parent = game.Workspace
                newPart.Position = hitPosition
                newPart.Transparency = 1
                newPart.CanCollide = false
                Attachment("Create", newPart)
                Attachment("Create", HumanoidRootPart)
                local Rope = Instance.new("RodConstraint")
                Rope.Parent = newPart
                Rope.Attachment0 = newPart.Attachment
                Rope.Attachment1 = HumanoidRootPart.Attachment
                Rope.Color = RopeColor
                Rope.Length = distance
                Rope.Visible = RopeVisible
                Rope.Thickness = 0.25
                local tween = game:GetService("TweenService"):Create(Rope, TweenInfo.new(0.6, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false), {Length = 0})
                tween:Play()
                newPart.Touched:Connect(function(touched)
                    if touched then
                        if touched == HumanoidRootPart then
                            wait(0.1)
                            Attachment("Destroy", newPart)
                            Attachment("Destroy", HumanoidRootPart)
                            newPart.RodConstraint:Destroy()
                            newPart:Destroy()
                        else
                            wait()
                        end
                    end
                end)
            end
            debounce = false
        end
    end)
    

    Hope this helps, Programming_Good_Games