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!!
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)