Game is full of these errors!

My game is full of these errors:

attempt to index nil with 'WaitForChild'
--and
 attempt to index nil with 'FindFirstChild'

Here are the scripts:

local LocalPLR = game.Players.LocalPlayer
local Char = LocalPLR.Character
local HRP = Char:WaitForChild("HumanoidRootPart")

HRP.ChildAdded:Connect(function(obj)
	if obj.ClassName == "BodyGyro" or obj.ClassName == "BodyAngularVelocity" or obj.ClassName == "BodyPosition" or obj.ClassName == "BodyVelocity" or obj.ClassName == "BodyThrust" then
		game.ReplicatedStorage.BanEvent:FireServer()
		warn("Exploiter was detected, and banned.")
	end
end)

This is just a simple anti-fly that runs on the client.

-- server sided one
if game.ServerScriptService:FindFirstChild("Anti-Fly"):FindFirstChild(tostring(Players[i])) ~= nil then

Char is nil, it still needs to load in. A simple fix is…

local Char = LocalPLR.Character or LocalPLR.CharacterAdded:Wait()
-- This gets it, or waits for the CharacterAdded event

This is because clients are wacky, and you can’t expect everything to be ok and loaded in time.

EDIT:

-- Second is because you are calling tostring on an instance, just do
... FindFirstChild(Players[i].Name)
1 Like

It doesn’t give me error’s but the script doesn’t work now.

On which one? Also, if it’s the server, it might be because it’s nil.

This one:

local LocalPLR = game.Players.LocalPlayer
local Char = LocalPLR.Character or LocalPLR.CharacterAdded:Wait()
local HRP = Char:WaitForChild("HumanoidRootPart")

HRP.ChildAdded:Connect(function(obj)
	if obj.ClassName == "BodyGyro" or obj.ClassName == "BodyAngularVelocity" or obj.ClassName == "BodyPosition" or obj.ClassName == "BodyVelocity" or obj.ClassName == "BodyThrust" then
		game.ReplicatedStorage.BanEvent:FireServer()
		warn("Exploiter was detected, and banned.")
	end
end)

Hmmm, try adding a print inside the HRP.ChildAdded connection, like

HRP.ChildAdded ...
print("Child has been added to HRP")