Wondering if this peice of code is safe to use

If not, could you please provide a better way to do it?

			local character = hit.Parent
			local player = Players.LocalPlayer
			if player then
				local humanoid = workspace:FindFirstChild(player.Name):FindFirstChild("Humanoid")
				if humanoid then
                  --ignore
					changegravityGUI()
					RemoteEvent:FireServer(196.2 * 0.38)
				end
			end

specifically FindFirstChild(player.name)

workspace:FindFirstChild(player.Name) is safe to do, but chaining another FindFirstChild immediately after is not. If there isnt anything in the workspace with the player’s name, then it will be nil. Then, you would be trying to find a child named Humanoid in nil, which will error.

Instead, check if the object in the workspace exists, THEN check if the humanoid exists within it:

    local object = workspace:FindFirstChild(player.Name)
    if object then
        local humanoid = object:FindFirstChild("Humanoid")
        if humanoid then

        end
    end

2 Likes