Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

Script.parent.local

2 answers

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)

2 comments

0

good job! solving your problem is one step closer to being a professional coder!

0

please add {solved} in the title btw

This discussion is preserved as it was. New answers, comments, and votes are closed.