Why does this fly script turn speed to nan?
Asked by
5 years ago Edited 5 years ago
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.
02 | local UserInputService = game:GetService( "UserInputService" ) |
03 | local RunService = game:GetService( "RunService" ) |
04 | local Players = game:GetService( "Players" ) |
07 | local LocalPlayer = Players.LocalPlayer |
08 | local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait() |
09 | local HumanoidRootPart = Character:WaitForChild( "HumanoidRootPart" ) |
10 | local CurrentCamera = workspace.CurrentCamera |
13 | local BodyVelocity = Instance.new( "BodyVelocity" ) |
14 | BodyVelocity.MaxForce = Vector 3. new( math.huge , math.huge , math.huge ) |
15 | BodyVelocity.Velocity = Vector 3. new( 0 , 0 , 0 ) |
16 | local BodyGyro = Instance.new( "BodyGyro" ) |
17 | BodyGyro.MaxTorque = Vector 3. new( math.huge , math.huge , math.huge ) |
20 | BodyGyro.CFrame = HumanoidRootPart.CFrame |
26 | BodyVelocity.Parent = HumanoidRootPart |
27 | BodyGyro.Parent = HumanoidRootPart |
30 | if status = = true then |
31 | if speed < maxSpeed then |
35 | local MoveDir = CFrame.new(CurrentCamera.CFrame.p, (CurrentCamera.CFrame * CFrame.new(xDelta, 0 , zDelta)).p) |
36 | BodyVelocity.Velocity = MoveDir.lookVector * speed |
41 | local MoveDir = CFrame.new(CurrentCamera.CFrame.p, (CurrentCamera.CFrame * CFrame.new(xDelta, 0 , zDelta)).p) |
42 | BodyVelocity.Velocity = MoveDir.lookVector * speed |
48 | UserInputService.InputBegan:Connect( function (InputObject) |
49 | local KeyCode = InputObject.KeyCode |
50 | if KeyCode = = Enum.KeyCode.W then |
52 | elseif KeyCode = = Enum.KeyCode.A then |
54 | elseif KeyCode = = Enum.KeyCode.S then |
56 | elseif KeyCode = = Enum.KeyCode.D then |
61 | UserInputService.InputEnded:Connect( function (InputObject) |
62 | local KeyCode = InputObject.KeyCode |
63 | if KeyCode = = Enum.KeyCode.W then |
65 | elseif KeyCode = = Enum.KeyCode.A then |
67 | elseif KeyCode = = Enum.KeyCode.S then |
69 | elseif KeyCode = = Enum.KeyCode.D then |
74 | RunService.RenderStepped:Connect( function () |
75 | BodyGyro.CFrame = CurrentCamera.CFrame |
76 | if xDelta ~ = 0 or zDelta ~ = 0 then |