Getting errors when a script works perfectly

Hey devforum, I made a plane script, but I have a problem, even though the script works perfectly I am getting these errors:
image

image

This is the script I am using:

local onEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlaneEvents"):WaitForChild("OnEvent")
local offEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlaneEvents"):WaitForChild("OnEvent")

onEvent.OnServerEvent:Connect(function(plr, mousePosition, speed, goDir, body, velMaxF, bgyroMaxT)
	if not body:FindFirstChild("BodyGyro") and not body:FindFirstChild("BodyVelocity") then
		local newBodyVel = Instance.new("BodyVelocity", body)
		local newBodyGyro = Instance.new("BodyGyro", body)
		newBodyVel.MaxForce = velMaxF
		newBodyVel.P = 2000
		newBodyVel.Velocity = body.CFrame.LookVector * speed
		newBodyGyro.MaxTorque = bgyroMaxT
		newBodyGyro.CFrame = CFrame.new(body.Position, mousePosition.Position)
	else
		body.BodyGyro.CFrame = CFrame.new(body.Position, mousePosition.Position)
		body.BodyVelocity.Velocity = body.CFrame.LookVector * speed
	end
end)

offEvent.OnServerEvent:Connect(function(plr, body)
	if body:FindFirstChild("BodyVelocity") and body:FindFirstChild("BodyGyro") then
		body.BodyVelocity:Destroy()
		body.BodyGyro:Destroy()
	end
end)

This may be a fix:

local onEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlaneEvents"):WaitForChild("OnEvent")
local offEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlaneEvents"):WaitForChild("OnEvent")

onEvent.OnServerEvent:Connect(function(plr, mousePosition, speed, goDir, body, velMaxF, bgyroMaxT)
	if body:FindFirstChild("BodyGyro") == nil and body:FindFirstChild("BodyVelocity") == nil then
		local newBodyVel = Instance.new("BodyVelocity", body)
		local newBodyGyro = Instance.new("BodyGyro", body)
		newBodyVel.MaxForce = velMaxF
		newBodyVel.P = 2000
		newBodyVel.Velocity = body.CFrame.LookVector * speed
		newBodyGyro.MaxTorque = bgyroMaxT
		newBodyGyro.CFrame = CFrame.new(body.Position, mousePosition.Position)
	else
		body.BodyGyro.CFrame = CFrame.new(body.Position, mousePosition.Position)
		body.BodyVelocity.Velocity = body.CFrame.LookVector * speed
	end
end)

offEvent.OnServerEvent:Connect(function(plr, body)
	if body:FindFirstChild("BodyVelocity") ~= nil and body:FindFirstChild("BodyGyro") ~= nil then
		body.BodyVelocity:Destroy()
		body.BodyGyro:Destroy()
	end
end)
1 Like

You might wanna check the order in wich the values of an event get returned. At line 5, the object body is nil. So either you’re sending nil, or it doesn’t exist.
On the second one, body is declared as a cframe. What you can do there is game.workspace[player.name] to get the character, and then work from there.

1 Like

Can we know what is being passed as “body” argument from the client? The thing is that you’re trying to FindFirstChild of a CFrame Value, whereas thats not even a method for CFrame values and in the first screenshot the body argument is basically nil.


@Jermartynojm You probably need to see the error more carefully, the error is in the :FindFirstChild method, its not a valid method of CFrame and in the first screenshot, the “body” argument is nil.

Also checking

if not body:FindFirstChild("BodyGyro")

and checking

if body:FindFirstChild("BodyGyro") == nil

is the same thing.

1 Like
offEvent.OnServerEvent:Connect(function(plr, body)

“body” is a CFrame value, read the error.

1 Like

This is the client part:

	if turnedOn == true then
		local goDir = planeModel.Body.CFrame.LookVector
		local basePosition = planeModel.Body.Position
		local planeBody = planeModel.Body
		local mousePosition = GetMouseHit(workspace)
		local velMaxF = Vector3.new(400000, 400000, 400000)
		local bGyroMaxT = Vector3.new(900000, 900000, 900000)
		
		onEvent:FireServer(mousePosition, planeSpeed, goDir, planeBody, velMaxF, bGyroMaxT)
	else
		local planeBody = planeModel.Body
		
		offEvent:FireServer(planeBody)
	end
1 Like

First of all I wanted to add that, do you realize both of your events reference the same event in your server script?

I would first let you fix that off, as it results in quite a bit of confusion and also check if its the case in client side.

2 Likes