Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this fly script turn speed to nan?

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

0

Can you quote the line of code that the error shows up at?

0

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

1 answer

Figured it out

-- Services
local input = game:GetService("UserInputService")  -- Gets input from player's keyboard
local rs = game:GetService("RunService")		   -- Handles the runtime of the game
local camera = game.Workspace.CurrentCamera

-- Player
local player = game:GetService("Players").LocalPlayer				-- Player
local character														-- Player's character
local root															-- Character's HumanoidRootPart
local humanoid														-- Character's Humanoid
player.CharacterAdded:connect(function(Char)
	character = Char
	root = Char:WaitForChild("HumanoidRootPart")					
	humanoid = Char:WaitForChild("Humanoid")					
end)

-- Body Properties
local bodyVel = Instance.new("BodyVelocity", script)				-- Create "BodyVelocity" in rootpart
bodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)		-- Infinite potential forces
bodyVel.Velocity = Vector3.new()									-- No velocity / not moving

-- Directional Values
local xDelta = 0													-- 1/0/-1 = left/not moving/right
local lastXDelta = 0
local zDelta = 0													-- 1/0/-1 = forwards/not moving/backwards
local lastZDelta = 0

-- Speed Settings
local maxSpeed = 200												-- Maximum total speed
local currentSpeed = 0												-- Current flying speed

-- Current Flying Status
local flying = false


-- Activate/Deactivate Flying
input.InputBegan:connect(function(keyPut)
	local key = keyPut.KeyCode										-- What key was pressed
	if key == Enum.KeyCode.F and flying == false then
		bodyVel.Parent = root
		bodyVel.Velocity = root.Velocity
		flying = true												-- Activate flying
	elseif key == Enum.KeyCode.F and flying == true then
		bodyVel.Parent = script
		flying = false
	end
end)

-- Flying controls
input.InputBegan:connect(function(keyPut)
	local key = keyPut.KeyCode
	
	if key == Enum.KeyCode.W then			-- Forward Movement
		zDelta = zDelta - 1
	elseif key == Enum.KeyCode.A then		-- Left Movement
		xDelta = xDelta - 1
	elseif key == Enum.KeyCode.S then		-- Backwards Movement
		zDelta = zDelta + 1
	elseif key == Enum.KeyCode.D then		-- Right Movement
		xDelta = xDelta + 1
	end
end)

input.InputEnded:connect(function(keyPut)
	local key = keyPut.KeyCode
	if key == Enum.KeyCode.W then
		zDelta = zDelta + 1
	elseif key == Enum.KeyCode.A then
		xDelta = xDelta + 1
	elseif key == Enum.KeyCode.S then
		zDelta = zDelta - 1
	elseif key == Enum.KeyCode.D then
		xDelta = xDelta - 1
	end
end)

-- Are you flying?
rs.RenderStepped:connect(function()
	if flying then
		if xDelta ~= 0 or zDelta ~= 0 then
			lastXDelta = xDelta
			local direction = CFrame.new(camera.CFrame.p, (camera.CFrame * CFrame.new(xDelta, 0, zDelta)).p)

			if currentSpeed < (maxSpeed - 5) then
				currentSpeed = currentSpeed + (0.01 * maxSpeed)
			elseif math.floor(currentSpeed) == maxSpeed or currentSpeed > maxSpeed then
				currentSpeed = maxSpeed
			end
			bodyVel.Velocity = direction.LookVector * currentSpeed
		else
			if currentSpeed > 5 then
				bodyVel.Velocity = bodyVel.Velocity / currentSpeed
				currentSpeed = currentSpeed - (.02 * currentSpeed) 
				bodyVel.Velocity = bodyVel.Velocity * currentSpeed
			else
				currentSpeed = 0
			end
		end
	end
end)

1 comment

0

You can also try finding new scripts like this one 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.

This discussion is preserved as it was. New answers, comments, and votes are closed.