I built upon a preexisting script (i forgot who) to resemble the one in Iron Man Simulator. Unfortunately when stopping/slowing down, the player's speed and position become nan.
--// Services
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
--// Service Objects
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CurrentCamera = workspace.CurrentCamera
--// Objects
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Velocity = Vector3.new(0,0,0)
local BodyGyro = Instance.new("BodyGyro")
BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
BodyGyro.D = 100
BodyGyro.P = 20000
BodyGyro.CFrame = HumanoidRootPart.CFrame
local xDelta = 0
local zDelta = 0
local maxSpeed = 200 -- Studs/Second
local speed = 0
BodyVelocity.Parent = HumanoidRootPart
BodyGyro.Parent = HumanoidRootPart
function fly(status)
if status == true then
if speed < maxSpeed then
wait()
speed = speed + 5
end
local MoveDir = CFrame.new(CurrentCamera.CFrame.p, (CurrentCamera.CFrame * CFrame.new(xDelta, 0, zDelta)).p)
BodyVelocity.Velocity = MoveDir.lookVector * speed
else
if speed ~= 0 then
wait()
speed = speed - 5
local MoveDir = CFrame.new(CurrentCamera.CFrame.p, (CurrentCamera.CFrame * CFrame.new(xDelta, 0, zDelta)).p)
BodyVelocity.Velocity = MoveDir.lookVector * speed
end
end
end
UserInputService.InputBegan:Connect(function(InputObject)
local KeyCode = InputObject.KeyCode
if KeyCode == Enum.KeyCode.W then
zDelta = zDelta - 1
elseif KeyCode == Enum.KeyCode.A then
xDelta = xDelta - 1
elseif KeyCode == Enum.KeyCode.S then
zDelta = zDelta + 1
elseif KeyCode == Enum.KeyCode.D then
xDelta = xDelta + 1
end
end)
UserInputService.InputEnded:Connect(function(InputObject)
local KeyCode = InputObject.KeyCode
if KeyCode == Enum.KeyCode.W then
zDelta = zDelta + 1
elseif KeyCode == Enum.KeyCode.A then
xDelta = xDelta + 1
elseif KeyCode == Enum.KeyCode.S then
zDelta = zDelta - 1
elseif KeyCode == Enum.KeyCode.D then
xDelta = xDelta - 1
end
end)
RunService.RenderStepped:Connect(function()
BodyGyro.CFrame = CurrentCamera.CFrame
if xDelta ~= 0 or zDelta ~= 0 then
fly(true)
else
fly(false)
end
end)

2 comments
Can you quote the line of code that the error shows up at?
The script doesn't produce any errors, but the player's speed and position become nan. I believe this is because when xDelta and zDelta, the direction variable at line 41 becomes nan