Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Solved] How do you make a tool that makes you fly?

Asked by 4 years ago
Edited 4 years ago

Hello,

I want to make a tool that when you hold click it will fly you in the direction you are facing. I have a script that kind of works, except that you need to let go of the mouse to fly in another direction. Does anyone know how to fix this?

Here is the localscript:

local tool = script.Parent
local Handle = tool:WaitForChild("Handle")

tool.Activated:Connect(function()
    local player = game.Players.LocalPlayer
    local mouse = player:GetMouse()
    local character = player.Character or player.CharacterAdded:Wait()
    local hrp = character:WaitForChild("HumanoidRootPart")
    local velocity = Instance.new("BodyVelocity", hrp)
    velocity.MaxForce = Vector3.new(math.huge/2,math.huge/2,math.huge/2)
    velocity.Velocity = mouse.Hit.LookVector*100
end)
tool.Deactivated:Connect(function()
    local player = game.Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()
    local hrp = character:WaitForChild("HumanoidRootPart")
    local children = hrp:GetChildren()
    for i = 1, #children do
        local child = children[i]
        if child:IsA("BodyVelocity") then
            child:Destroy()
        end
    end
end)

Thank you!!

0
Script.parent.local betsie500 0 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Fixed it, I just need to add a while loop.

local tool = script.Parent
local Handle = tool:WaitForChild("Handle")

local flying = false

tool.Activated:Connect(function()
    flying = true
    local player = game.Players.LocalPlayer
    local mouse = player:GetMouse()
    local character = player.Character or player.CharacterAdded:Wait()
    local hrp = character:WaitForChild("HumanoidRootPart")
    local velocity = Instance.new("BodyVelocity", hrp)
    velocity.MaxForce = Vector3.new(math.huge/100,math.huge/100,math.huge/100)
    while flying do
        velocity.Velocity = mouse.Hit.LookVector*100
        tool.Deactivated:Connect(function()
            flying = false
        end)
        wait()
    end
end)
tool.Deactivated:Connect(function()
    flying = false
    local player = game.Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()
    local hrp = character:WaitForChild("HumanoidRootPart")
    local children = hrp:GetChildren()
    for i = 1, #children do
        local child = children[i]
        if child:IsA("BodyVelocity") then
            child:Destroy()
        end
    end
end)
0
good job! solving your problem is one step closer to being a professional coder! raid6n 2196 — 4y
0
please add {solved} in the title btw raid6n 2196 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Oh watch scripting vids

Answer this question