Attempt to index nil with 'CFrame'

I’m trying to write a door script using Collection Service and its not working please help

local Collection = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")

local DoorModel = Collection:GetTagged("Door")

local Doortweeninfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

local DooropenTween = TweenService:Create(DoorModel.DoorPart, Doortweeninfo, {CFrame = DoorModel.DoorFrame2.CFrame})
local DoorcloseTween = TweenService:Create(DoorModel.DoorPart, Doortweeninfo, {CFrame = DoorModel.DoorFrame1.CFrame})

for i, DoorModel in pairs(DoorModel) do
	DoorModel.Click.MouseClick:Connect(function()
		DoorModel.door_open:Play()
	end)
end

I tagged the doors Model not the parts inside the model I checked and the tag is spelled correctly and everything else is correct but the script is not finding the CFrame of one of the door frames to make the tween!

1 Like

You’re referencing the door model before creating the variable in the for loop.

In DooropenTween and DoorcloseTween you’re referencing the list of tagged items mistakingly as a door.

DoorModel is not a Model or any Instance in general, it is the returned list from GetTagged
The list is an array of Instances {Instance} and not an Instance such as a Model.

Sorry but I’m a little bit confused on that

GetTagged returns a table, such as {}

The value returned is referenced in your script as DoorModel

DoorModel.DoorPart is nil
DoorModel.DoorFrame1 is nil
DoorModel.DoorFrame2 is nil

(This value is nil).CFrame causes the error

nil means nothing, it doesn’t exist
You can’t find .CFrame in something that’s nothing, you can only find .CFrame in a BasePart

The reason those three above are nil is because you think DoorModel is a Model probably, but DoorModel probably looks more like:
{
[1] = Anything you gave the tag
[2] = Something else you might have given the tag
}

I do I fix the script using this information?

Is there any specific reason you want to use CollectionService?
Is this a server side or client side script?
What objects do you have marked using the tag “Door” can you show their hierarchy in Explorer?

I want to use the collection Service because most doors in my game have their own scripts and I don’t want to have to edit one by one I tagged the Model of the Door with all the parts inside the script is server-sided and the objects marked using the tag Door is the Doors model with all the parts inside of it

In your script, DoorModel is an array that holds your DoorModel, not the DoorModel itself.

You should consider creating the tweens inside of the iteration/loop

Something like:

local Collection: CollectionService = game:GetService("CollectionService")
local TweenService: TweenService = game:GetService("TweenService")

local DoorModels: {Instance} = Collection:GetTagged("Door")

local Doortweeninfo: TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

for _: number, DoorModel: Instance in pairs(DoorModels) do
  local Open: Tween = TweenService:Create(DoorModel.DoorPart, Doortweeninfo, {CFrame = DoorModel.DoorFrame2.CFrame})
  local Close: Tween = TweenService:Create(DoorModel.DoorPart, Doortweeninfo, {CFrame = DoorModel.DoorFrame1.CFrame})
  local OpenState: boolean = false
  DoorModel.Click.MouseClick:Connect(function(): ()
    OpenState = not OpenState
    if OpenState then
      Open:Play()
    else
      Close:Play()
    end
  end)
end


It’s erroring

Oh yeah sorry, that may be because I made a slight formatting mistake thanks to scripting on my phone; I will edit my mistake

Oh okay I will let you take your time!

It is already done; that slight formatting mistake was very easy to fix.

Wheres the code at? you busy trying to post it?

local Collection = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")

local Doortweeninfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

for _, DoorModel in pairs(Collection:GetTagged("Door")) do
    local DooropenTween = TweenService:Create(DoorModel.DoorPart, Doortweeninfo, {CFrame = DoorModel.DoorFrame2.CFrame})
    local DoorcloseTween = TweenService:Create(DoorModel.DoorPart, Doortweeninfo, {CFrame = DoorModel.DoorFrame1.CFrame})
    local IsOpened = false
    
	DoorModel.Click.MouseClick:Connect(function()
        if IsOpened == false then
            IsOpened = true
		    DooropenTween:Play()
        if IsOpened == true then
            IsOpened = false
		    DoorcloseTween:Play()
        end
	end)
end

I edited my text above like I said I would edit it

My code also remembers its opening state so the door will close at the second interaction

It works!!! thank you so much!!! I’m putting your name in the credits!

1 Like

Sorry you were too late!! but thanks for trying!

Your name is now in the credits: Baldi's Basics hide and seek multi-player - Roblox

1 Like