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

Is there anyway to change this to use BodyGyro and BodyVelocity?

Asked by 6 years ago

Is there anyway I can change this script to use BodyGyro and BodyVelocity? This script is supposed to let you noclip when you press "V" but it doesn't update the position properly in FilteringEnabled.

local player = game.Players.LocalPlayer
local selected = false
local XboxPos = Vector3.new(0,0,0)
local c = workspace.CurrentCamera
local userInput = game:GetService("UserInputService")
local speed = 60
local lastUpdate = 0

function getNextMovement(deltaTime)
    local nextMove = Vector3.new()
    -- Left/Right
        if userInput:IsKeyDown("A") or userInput:IsKeyDown("Left") then
            nextMove = Vector3.new(-1,0,0)
        elseif userInput:IsKeyDown("D") or userInput:IsKeyDown("Right") then
            nextMove = Vector3.new(1,0,0)
        end
    -- Forward/Back
        if userInput:IsKeyDown("W") or userInput:IsKeyDown("Up") then
            nextMove = nextMove + Vector3.new(0,0,-1)
        elseif userInput:IsKeyDown("S") or userInput:IsKeyDown("Down") then
            nextMove = nextMove + Vector3.new(0,0,1)
        end
    -- Up/Down
        if userInput:IsKeyDown("Space") then
            nextMove = nextMove + Vector3.new(0,1,0)
        elseif userInput:IsKeyDown("LeftControl") then
            nextMove = nextMove + Vector3.new(0,-1,0)
        end
        nextMove = nextMove + XboxPos
    return CFrame.new( nextMove * (speed * deltaTime) )
end

function onSelected()
local char = player.Character
    if char then
        local humanoid = char:WaitForChild("Humanoid")
        local root = char:WaitForChild("HumanoidRootPart")
        currentPos = root.Position
        selected = true
        root.Anchored = true
        lastUpdate = tick()
        humanoid.PlatformStand = true
        while selected do
            wait()
            local delta = tick()-lastUpdate
            local look = (c.Focus.p-c.CoordinateFrame.p).unit
            local move = getNextMovement(delta)
            local pos = root.Position
            root.CFrame = CFrame.new(pos,pos+look) * move
            lastUpdate = tick()
        end
        root.Anchored = false
        root.Velocity = Vector3.new()
        humanoid.PlatformStand = false
    end
end

game:GetService("UserInputService").InputBegan:connect(function(input,event)
    if event then return end
    if input.KeyCode == Enum.KeyCode.ButtonB or input.KeyCode == Enum.KeyCode.V then
        if (selected == false) then
            selected = true
            onSelected()
        elseif (selected == true) then
            selected = false
        end
    end
end)

I would want the script to use this code with BodyGyro and BodyVelocity since it works in FilteringEnabled. I just don't know how to change it to be used in the script above.

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:findFirstChild("Torso") and game.Players.LocalPlayer.Character:findFirstChild("Humanoid") 
local mouse = game.Players.LocalPlayer:GetMouse() 
repeat wait() until mouse
local plr = game.Players.LocalPlayer 
local torso = plr.Character.Torso 
local flying = false 
local ctrl = {f = 0, b = 0, l = 0, r = 0} 
local lastctrl = {f = 0, b = 0, l = 0, r = 0}  
local speed = 60 
function Fly()
local bg = Instance.new("BodyGyro", torso) 
bg.P = 9e4 
bg.maxTorque = Vector3.new(9e9, 9e9, 9e9) 
bg.cframe = torso.CFrame 
local bv = Instance.new("BodyVelocity", torso) 
bv.velocity = Vector3.new(0,0,0) 
bv.maxForce = Vector3.new(9e9, 9e9, 9e9) 
repeat wait() 
plr.Character.Humanoid.PlatformStand = true 
if (ctrl.l + ctrl.r) ~= 0 or (ctrl.f + ctrl.b) ~= 0 then 
bv.velocity = ((game.Workspace.CurrentCamera.CoordinateFrame.lookVector * (ctrl.f+ctrl.b)) + ((game.Workspace.CurrentCamera.CoordinateFrame * CFrame.new(ctrl.l+ctrl.r,(ctrl.f+ctrl.b)*0,0).p) - game.Workspace.CurrentCamera.CoordinateFrame.p))*60
lastctrl = {f = ctrl.f, b = ctrl.b, l = ctrl.l, r = ctrl.r} 
elseif (ctrl.l + ctrl.r) == 0 and (ctrl.f + ctrl.b) == 0 and speed ~= 0 then 
bv.velocity = ((game.Workspace.CurrentCamera.CoordinateFrame.lookVector * (lastctrl.f+lastctrl.b)) + ((game.Workspace.CurrentCamera.CoordinateFrame * CFrame.new(lastctrl.l+lastctrl.r,(lastctrl.f+lastctrl.b)*0,0).p) - game.Workspace.CurrentCamera.CoordinateFrame.p))*0 
else 
bv.velocity = Vector3.new(0,0,0) 
end 
bg.cframe = game.Workspace.CurrentCamera.CoordinateFrame 
until not flying 
ctrl = {f = 0, b = 0, l = 0, r = 0} 
lastctrl = {f = 0, b = 0, l = 0, r = 0} 
speed = 0 
bg:Destroy() 
bv:Destroy() 
plr.Character.Humanoid.PlatformStand = false 
end 
mouse.KeyDown:connect(function(key) 
if key:lower() == "" then 
if flying then flying = false
else 
flying = true
Fly() 
end 
elseif key:lower() == "w" then 
ctrl.f = 1 
elseif key:lower() == "s" then 
ctrl.b = -1 
elseif key:lower() == "a" then 
ctrl.l = -1 
elseif key:lower() == "d" then 
ctrl.r = 1 
end 
end) 
mouse.KeyUp:connect(function(key) 
if key:lower() == "w" then 
ctrl.f = 0 
elseif key:lower() == "s" then 
ctrl.b = 0 
elseif key:lower() == "a" then 
ctrl.l = 0 
elseif key:lower() == "d" then 
ctrl.r = 0 
end 
end)
Fly()

Thanks for your help.

Answer this question