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

How would I add a hotkey to this fly script?

Asked by
Simxzn 2
4 years ago
Edited 4 years ago

How would I be able to add a hotkey to this fly script?

local Fly = true

    if Fly == true then

        Fly = false
        return

    end

    Fly = true

  local mouse=game.Players.LocalPlayer:GetMouse''

  localplayer=game.Players.LocalPlayer

  game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

  local torso = game.Players.LocalPlayer.Character.HumanoidRootPart

  local speed=0

  local keys={a=false,d=false,w=false,s=false}

  local e1

  local e2

  local function start()

   local pos = Instance.new("BodyPosition",torso)

   local gyro = Instance.new("BodyGyro",torso)

   pos.Name="EPIXPOS"

   pos.maxForce = Vector3.new(math.huge, math.huge, math.huge)

   pos.position = torso.Position

   gyro.maxTorque = Vector3.new(9e9, 9e9, 9e9)

   gyro.cframe = torso.CFrame

   repeat

    wait()

    localplayer.Character.Humanoid.PlatformStand=true

    local new=gyro.cframe - gyro.cframe.p + pos.position

    if not keys.w and not keys.s and not keys.a and not keys.d then

     speed=1

    end

    if keys.w then

     new = new + workspace.CurrentCamera.CoordinateFrame.lookVector * speed

     speed=speed+0.01

    end

    if keys.s then

     new = new - workspace.CurrentCamera.CoordinateFrame.lookVector * speed

     speed=speed+0.01

    end

    if keys.d then

     new = new * CFrame.new(speed,0,0)

     speed=speed+0.01

    end

    if keys.a then

     new = new * CFrame.new(-speed,0,0)

     speed=speed+0.01

    end

    if speed>5 then

     speed=5

    end

    pos.position=new.p

    if keys.w then

     gyro.cframe = workspace.CurrentCamera.CoordinateFrame*CFrame.Angles(-math.rad(speed*15),0,0)

    elseif keys.s then

     gyro.cframe = workspace.CurrentCamera.CoordinateFrame*CFrame.Angles(math.rad(speed*15),0,0)

    else

     gyro.cframe = workspace.CurrentCamera.CoordinateFrame

    end

   until not Fly

   if gyro then gyro:Destroy() end

   if pos then pos:Destroy() end

   flying=false

   localplayer.Character.Humanoid.PlatformStand=false

   speed=0

  end

  e1=mouse.KeyDown:connect(function(key)

   if not torso or not torso.Parent then flying=false e1:disconnect() e2:disconnect() return end

   if key=="w" then

    keys.w=true

   elseif key=="s" then

    keys.s=true

   elseif key=="a" then

    keys.a=true

   elseif key=="d" then

    keys.d=true

   end

  end)

  e2=mouse.KeyUp:connect(function(key)

   if key=="w" then

    keys.w=false

   elseif key=="s" then

    keys.s=false

   elseif key=="a" then

    keys.a=false

   elseif key=="d" then

    keys.d=false

   end

  end)

  start()
0
UserInputService AizakkuZ 226 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Since Mouse.KeyDown is deprecated, you must use UserInputService. Also, I remade your code to look nicer and more readable.

local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local e1
local e2
local Fly = false
local Speed = 0
local keys = {a = false,d=false,w=false,s=false}

local function start()
    if not Fly then
        Fly = true
    else
        Fly = false
    end

    local pos = Instance.new("BodyPosition")
    local gyro = Instance.new("BodyGyro")

    pos.Name = "EPIXPOS"
    pos.maxForce = Vector3.new(math.huge, math.huge, math.huge)
    pos.position = HumanoidRootPart.Position
    gyro.maxTorque = Vector3.new(9e9, 9e9, 9e9)
    gyro.cframe = HumanoidRootPart.CFrame

    pos.Parent = HumanoidRootPart
    gyro.Parent = HumanoidRootPart
    repeat
        wait()
        Character.Humanoid.PlatformStand = true
        local new = gyro.cframe - gyro.cframe.p + pos.position
    if not keys.w and not keys.s and not keys.a and not keys.d then
        Speed = 1
    end

    if keys.w then
        new = new + workspace.CurrentCamera.CoordinateFrame.lookVector * Speed
        Speed=Speed+0.01
    end

    if keys.s then
        new = new - workspace.CurrentCamera.CoordinateFrame.lookVector * Speed
        Speed = Speed+0.01
    end

    if keys.d then
        new = new * CFrame.new(Speed,0,0)
        Speed=Speed+0.01
    end

    if keys.a then
        new = new * CFrame.new(-Speed,0,0)
        Speed = Speed+0.01
    end

    if Speed > 5 then
        Speed = 5
    end

    pos.position=new.p

    if keys.w then
        gyro.cframe = workspace.CurrentCamera.CoordinateFrame*CFrame.Angles(-math.rad(Speed*15),0,0)
    elseif keys.s then
        gyro.cframe = workspace.CurrentCamera.CoordinateFrame*CFrame.Angles(math.rad(Speed*15),0,0)
    else
        gyro.cframe = workspace.CurrentCamera.CoordinateFrame
    end

    until not Fly

    if gyro then gyro:Destroy() end
    if pos then pos:Destroy() end
    flying = false
    Character.Humanoid.PlatformStand=false
    Speed = 0
end

UserInputService.InputBegan:Connect(function(Input, GameProcess)
    if GameProcess then return end
    if not HumanoidRootPart or not HumanoidRootPart.Parent then flying = false e1:disconnect() e2:disconnect() return end
    if Input.KeyCode == Enum.KeyCode.W then
        keys.w = true
    elseif Input.KeyCode == Enum.KeyCode.S then
        keys.s = true
    elseif Input.KeyCode == Enum.KeyCode.A then
        keys.a = true
    elseif Input.KeyCode == Enum.KeyCode.D then
        keys.d = true
    end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcess)
    if GameProcess then return end
    if not HumanoidRootPart or not HumanoidRootPart.Parent then flying = false e1:disconnect() e2:disconnect() return end
    if Input.KeyCode == Enum.KeyCode.W then
        keys.w = false
    elseif Input.KeyCode == Enum.KeyCode.S then
        keys.s = false
    elseif Input.KeyCode == Enum.KeyCode.A then
        keys.a = false
    elseif Input.KeyCode == Enum.KeyCode.D then
        keys.d = false
    end
end)

UserInputService.InputBegan:Connect(function(Input, GameProcess)
    if GameProcess then return end
    if Input.KeyCode == Enum.KeyCode.F then -- Change "F" to the letter for activating the fly
        start()
    end
end)

If it helped, accept the answer! :D

0
Thank you so much man! Simxzn 2 — 4y
0
No worries! Happy to help :D XviperIink 428 — 4y
Ad
Log in to vote
0
Answered by
AizakkuZ 226 Moderation Voter
4 years ago
Edited 4 years ago

UserInputService Mouse.KeyDown is deprecated. Also, I noticed this was all one script so, here is a link to the roblox dev documentation.

https://developer.roblox.com/en-us/api-reference/class/UserInputService

Roblox's Server to Client model https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

Answer this question