(this is a local script) (tool required handle is off)
local UIS = game:GetService("UserInputService")
local last = tick()
local isFlying = false
local Speed = 32
local Tool = script.Parent
local Character = script.Parent.Parent
local HumanoidPart = Character:WaitForChild("HumanoidRootPart")
Tool.Equipped:Connect(function()
local function flying(isFlying)
if isFlying then
local BodyV = Instance.new("BodyVelocity")
local VectorV = Instance.new("VectorForce")
local Force = 3500
local Attachment = Instance.new("Attachment")
VectorV.Attachment0 = Attachment
wait(3)
VectorV.Force = Vector3.new(0,math.sin(math.rad(60)),-math.cos(math.rad(60))).Unit*Force
BodyV.MaxForce = Vector3.new(0,1,0) * 10000
BodyV.Velocity = Vector3.new(0,0,0)
BodyV.Name = "Vel"
VectorV.Name = "VecV"
Attachment.Name = "Atta"
Attachment.Parent = HumanoidPart
BodyV.Parent = HumanoidPart
VectorV.Parent = HumanoidPart
else
local B = HumanoidPart:FindFirstChild("Vel")
local V = HumanoidPart:FindFirstChild("VecV")
local A = HumanoidPart:FindFirstChild("Atta")
if B then B:Destroy() end
if V then V:Destroy() end
if A then A:Destroy() end
end
end
UIS.InputBegan:Connect(function(input,proccessed)
if proccessed then return end
if input.KeyCode == Enum.KeyCode.E then
if (tick() - last) < 0.5 then
isFlying = not isFlying
flying(isFlying)
elseif isFlying then
local B = HumanoidPart:FindFirstChild("Vel")
if B then B.Velocity = Vector3.new(0,1,0) * Speed end
end
last = tick()
end
if input.KeyCode == Enum.KeyCode.Q then
if isFlying then
local B = HumanoidPart:FindFirstChild("Vel")
if B then B.Velocity = Vector3.new(0,-1,0) * Speed end
end
end
end)
UIS.InputEnded:Connect(function(input,processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.Q then
if isFlying then
local B = HumanoidPart:FindFirstChild("Vel")
if B then B.Velocity = Vector3.new(0,0,0)end
end
end
end)
end)
The reason it doesn’t work is that a LocalScript only runs when it is located somewhere directly related to the LocalPlayer, such as StarterGui, ReplicatedFirst, StarterPlayerScripts and StarterCharacterScripts.
Local scripts work fine in StarterPack even if it’s the child of a tool. The problem is with your script itself, particularly this line.
local Character = script.Parent.Parent
“script.Parent.Parent” refers to the player’s backpack, not the player’s character. This is a simple fix though, you can easily replace that line with these two to get the character properly.
local Player = Tool.Parent.Parent
local Character = Player.Character or Player.CharacterAdded:Wait()
However, there are still more problems with the script. It’s very bad to make connections for InputBegan and InputEnded each time the tool is equipped, so instead you should use a boolean for whether or not the tool is equipped using the Equipped and Unequipped events. I’ve done that for you and fixed the character problem, however the flight itself seems to be buggy. Let me know if you have any questions or need any more help.
local UIS = game:GetService("UserInputService")
local last = tick()
local isEquipped = false
local isFlying = false
local Speed = 32
local Tool = script.Parent
local Player = Tool.Parent.Parent
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidPart = Character:WaitForChild("HumanoidRootPart")
local function flying(isFlying)
if isFlying then
local BodyV = Instance.new("BodyVelocity")
local VectorV = Instance.new("VectorForce")
local Force = 3500
local Attachment = Instance.new("Attachment")
VectorV.Attachment0 = Attachment
wait(3)
VectorV.Force = Vector3.new(0,math.sin(math.rad(60)),-math.cos(math.rad(60))).Unit*Force
BodyV.MaxForce = Vector3.new(0,1,0) * 10000
BodyV.Velocity = Vector3.new(0,0,0)
BodyV.Name = "Vel"
VectorV.Name = "VecV"
Attachment.Name = "Atta"
Attachment.Parent = HumanoidPart
BodyV.Parent = HumanoidPart
VectorV.Parent = HumanoidPart
else
local B = HumanoidPart:FindFirstChild("Vel")
local V = HumanoidPart:FindFirstChild("VecV")
local A = HumanoidPart:FindFirstChild("Atta")
if B then B:Destroy() end
if V then V:Destroy() end
if A then A:Destroy() end
end
end
Tool.Equipped:Connect(function()
isEquipped = true
end)
Tool.Unequipped:Connect(function()
isEquipped = false
end)
UIS.InputBegan:Connect(function(input,proccessed)
if proccessed or not isEquipped then return end
if input.KeyCode == Enum.KeyCode.E then
if (tick() - last) < 0.5 then
isFlying = not isFlying
flying(isFlying)
elseif isFlying then
local B = HumanoidPart:FindFirstChild("Vel")
if B then B.Velocity = Vector3.new(0,1,0) * Speed end
end
last = tick()
end
if input.KeyCode == Enum.KeyCode.Q then
if isFlying then
local B = HumanoidPart:FindFirstChild("Vel")
if B then B.Velocity = Vector3.new(0,-1,0) * Speed end
end
end
end)
UIS.InputEnded:Connect(function(input,processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.Q then
if isFlying then
local B = HumanoidPart:FindFirstChild("Vel")
if B then B.Velocity = Vector3.new(0,0,0)end
end
end
end)
Ouuter was checking the “gameProcessed” argument for InputBegan. You normally do want to return if gameProcessed is true, because otherwise it will pick up on undesirable input such as typing in chat.
An example of using the gameProcessed parameter? You used it properly before, it caused no problems in the script. Now the problems are just with the flight logic itself, but here’s an example of using input began properly if that’s what you meant.
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
return -- if the gameProcessed argument is true, don't execute whatever happens in this block
end
-- code to execute on input began
end)