Attempt to perform arithmetic (add) on nil problem

Hi, I have a fly tool:
when equipped, it’s going to fire an event from a server script
and the local script is going to receive it, and run the fly function, the same applies when unequipped.

--> Player
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")

--> Booleans
local flying = false

--> Fly Settings
local controls = {front = 0, back = 0, left = 0, right = 0}
local lastControls = {front = 0, back = 0, left = 0, right = 0}
local maxspeed = 400 
local speed = 5000 

--> Tool Events
local replicatedStorage = game:GetService("ReplicatedStorage")
local equipped = replicatedStorage["Ethereal Fly Ability"].Events.equipped
local unequipped = replicatedStorage["Ethereal Fly Ability"].Events.unequipped

function fly() 
	local bodyGyro = Instance.new("BodyGyro", humanoidRootPart) 
	bodyGyro.P = 9e4 
	bodyGyro.MaxTorque = Vector3.new(9e9, 9e9, 9e9) 
	bodyGyro.CFrame = humanoidRootPart.CFrame 
	local bodyVelocity = Instance.new("BodyVelocity", humanoidRootPart) 
	bodyVelocity.Velocity = Vector3.new(0,0.1,0)
	bodyVelocity.MaxForce = Vector3.new(9e9, 9e9, 9e9) 
	repeat wait()
		humanoid.PlatformStand = true 
		if controls.left + controls.right ~= 0 or controls.front + controls.back ~= 0 then  --// Error Line
			speed = speed + 0.5 + (speed/maxspeed) 
			if speed > maxspeed then 
				speed = maxspeed 
			end 

		elseif not (controls.left + controls.right ~= 0 or controls.front + controls.back ~= 0) and speed ~= 0 then 
			speed = speed - 1 
			if speed < 0 then 
				speed = 0 
			end 
		end 

		if (controls.left + controls.right) ~= 0 or (controls.front + controls.back) ~= 0 then
			bodyVelocity.Velocity = ((camera.CoordinateFrame.LookVector * (controls.front + controls.back)) + ((camera.CoordinateFrame * CFrame.new(controls.left + controls.right ,(controls.front + controls.back)*.2,0).Position) - camera.CoordinateFrame.Position)) * speed 
		elseif (controls.left + controls.right) == 0 and (controls.front + controls.back) == 0 and speed ~= 0 then 
			bodyVelocity.velocity = ((game.Workspace.CurrentCamera.CoordinateFrame.lookVector * (lastControls.front + lastControls.back)) + ((game.Workspace.CurrentCamera.CoordinateFrame * CFrame.new(lastControls.left + lastControls.right, (lastControls.front + lastControls.back) * 0.2, 0).Position) - game.Workspace.CurrentCamera.CoordinateFrame.Position)) * speed 
		else 
			bodyVelocity.Velocity = Vector3.new(0,0.1,0) 
		end 
		bodyGyro.CFrame = camera.CoordinateFrame * CFrame.Angles(-math.rad((controls.front+controls.back)*50*speed/maxspeed),0,0) 
	until not flying 
	controls = {f = 0, b = 0, l = 0, r = 0} 
	lastControls = {f = 0, b = 0, l = 0, r = 0} 
	speed = 0 
	bodyGyro:Destroy() 
	bodyVelocity:Destroy() 
	humanoid.PlatformStand = false 
end

--> Equipped
equipped.OnClientEvent:Connect(function()
	if flying then 
		flying = false 
	else 
		flying = true 
		fly() 
	end	
	mouse.KeyDown:connect(function(key) 
		if key:lower() == "w" then 
			controls.f = 1 
		elseif key:lower() == "s" then 
			controls.b = -1 
		elseif key:lower() == "a" then 
			controls.l = -1 
		elseif key:lower() == "d" then 
			controls.r = 1 
		end
	end)
end)

--> Unequipped
unequipped.OnClientEvent:Connect(function()
	flying = false
	mouse.KeyUp:connect(function(key) 
		if key:lower() == "w" then 
			controls.f = 0 
		elseif key:lower() == "s" then 
			controls.b = 0 
		elseif key:lower() == "a" then 
			controls.l = 0 
		elseif key:lower() == "d" then 
			controls.r = 0 
		end 
	end)
end)

I don’t really know how to fix that. I tried changing the values local controls = {front = 0.001, back = 0.001, left = 0.001, right = 0.001} to this instead of all 0’s so they won’t be defined as nil[?]

Looks like this is the problem:
controls = {f = 0, b = 0, l = 0, r = 0} you reassign controls to have different key-values after the loop, so the next call to the function is trying to reference controls.left when its been renamed controls.l
similarly in another function you’re assinging controls f,b,l,r again

1 Like

is it this part that renamed the controls.l

mouse.KeyDown:connect(function(key) 
		if key:lower() == "w" then 
			controls.f = 1 
		elseif key:lower() == "s" then 
			controls.b = -1 
		elseif key:lower() == "a" then 
			controls.l = -1 
		elseif key:lower() == "d" then 
			controls.r = 1 
		end
	end)

mouse.KeyUp:connect(function(key) 
		if key:lower() == "w" then 
			controls.front = 0 
		elseif key:lower() == "s" then 
			controls.back = 0 
		elseif key:lower() == "a" then 
			controls.left = 0 
		elseif key:lower() == "d" then 
			controls.right = 0 
		end 
	end)

Edit:
oh didn’t realize there was also this one:

controls = {f = 0, b = 0, l = 0, r = 0} 
lastControls = {f = 0, b = 0, l = 0, r = 0} 
1 Like