Attempt to index nil with 'CFrame'

  1. What do you want to achieve? I am trying to make a script where it controls every dropper in the game all under one script.

  2. What is the issue? The error keeps saying * `attempt to index nil with ‘CFrame’ * on line 16 even though the CFrame of the primary part is not nil.

  3. What solutions have you tried so far? I’ve tried looking for solutions on the devforum but I couldn’t find any that would work.

Code:

local CollectionService = game:GetService("CollectionService")
local Tycoons = game.Workspace.Tycoons
local module = require(game.ServerScriptService.TycoonModule)

for _, dropper in pairs(CollectionService:GetTagged("Dropper")) do
	local Tycoon = dropper.Parent.Parent
	local BillboardGUI = game.ReplicatedStorage.BillboardGui

	local DropperModule = require(game.ServerScriptService.DropperComponents[dropper.Name])

	while true do
		local purchasesFolder = Tycoon:FindFirstChild("Purchases")
		local DropperPartsFolder = Tycoon:FindFirstChild("DropperParts")

		if dropper:IsDescendantOf(purchasesFolder) then
			if dropper.PrimaryPart.CFrame ~= nil then
				module.CreatePart(dropper, BillboardGUI, DropperPartsFolder, DropperModule.Size, DropperModule.Color, DropperModule.Material, DropperModule.Value)
				print("spawned")
			else
				continue
			end
		end
		task.wait(DropperModule.Cooldown)
	end
end

Do all tagged instances of dropper have a PrimaryPart?

where are you trying to run this script?

Your error indicates that you are trying to find CFrame in nil so nil.CFrame.
Are you sure that dropper, assuming that it is a model, has a PrimaryPart set?

yes, all the droppers have a primary part set

yes, it has a primary part set in the model

i am running this script in server script service

Just check if the dropper has a PrimaryPart on line 16.
If it doesn’t, then you can add procedures to fix that.

I tried that and the script wasn’t erroring but it was not doing anything even though the droppers have a primary part set

btw sorry for the late reply

Try adding some debug prints to check if the Primary part isn’t getting changed by another script or if the dropper still even exists.
Let us know what the output is after like 30 seconds of running the game. :+1:

local CollectionService = game:GetService("CollectionService")
local Tycoons = game.Workspace.Tycoons
local module = require(game.ServerScriptService.TycoonModule)

for _, dropper in pairs(CollectionService:GetTagged("Dropper")) do
	local Tycoon = dropper.Parent.Parent
	local BillboardGUI = game.ReplicatedStorage.BillboardGui

	local DropperModule = require(game.ServerScriptService.DropperComponents[dropper.Name])

	while true do
		local purchasesFolder = Tycoon:FindFirstChild("Purchases")
		local DropperPartsFolder = Tycoon:FindFirstChild("DropperParts")
        if dropper then
            print("[0] Got dropper |",dropper:GetFullName())
        else
            print("[1] Failed to get dropper.")
        end

		if dropper:IsDescendantOf(purchasesFolder) then
            print("[0] Dropper is part of PurchasesFolder |",dropper:GetFullName())
            print("[0] Current PrimaryPart (With CFrame) |",dropper.PrimaryPart,tostring(dropper.Primary.CFrame))
			if dropper.PrimaryPart.CFrame ~= nil then
				module.CreatePart(dropper, BillboardGUI, DropperPartsFolder, DropperModule.Size, DropperModule.Color, DropperModule.Material, DropperModule.Value)
				print("spawned")
			else
				continue
                print("[1] Primary part CFrame is not correct.")
			end
        else
            print("[1] Dropper is not part of PurchaseFolder anymore.")

		end
		task.wait(DropperModule.Cooldown)
	end

Have you tried Cframe.new() ???

This might work by adding an and to see if it actually has a primarypart

if dropper.PrimaryPart and dropper.PrimaryPart.CFrame ~= nil then

Try changing

if dropper.PrimaryPart.CFrame ~= nil then

to

if dropper.PrimaryPart then

before i claim the tycoon, it repeats

[0] Got dropper | Dropper2 - Server - New:17
16:50:05.798
[0] Dropper is part of PurchasesFolder | Dropper2

after i claim a tycoon, it now prints

[0] Got dropper | Dropper2 - Server - New:17
16:50:05.798
[0] Dropper is part of PurchasesFolder | Dropper2
ServerScriptService.TycoonScripts.New:23: attempt to index nil with ‘CFrame’

then after the error the whole script just stops

Are you sure the dropper is part in workspace?
Since the debug print was “GetFullName()” And it was meant to show “game.Workspace…” But it just shows Dropper2. Meaning the dropper might be in nil, Indeed meaning it cannot find a Primary part.
Maybe try setting the dropper to workspace.

1 Like