Hello, recently i’ve discovered a weird glitch revolving FindFirstChild(). I keep getting this error saying “attempt to index nil with ‘FindFirstChild’” even though the instance is clearly there.
My code is simple, if the script finds a child named “Frame(number)” then it’ll tween the part to it.
If it doesnt, the script will just destroy itself.
local frame = 1
while true do
if not script.Parent:FindFirstChild("Frame"..frame) then
script:Destroy()
end
if script.Parent:FindFirstChild("Frame"..frame) then
task.wait(script.Parent:FindFirstChild("Frame"..frame).TimeBetweenFrames.Value - 0.025)
game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(0.075, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = script.Parent:FindFirstChild("Frame"..frame).Value}):Play()
frame = frame + 1
end
task.wait(0.025)
end
I’ve tried putting a wait to give the instance some time to load, but for some reason it still gives me the error despite being here.
(This is in a viewport by the way.)
regarding* anyway the error means you are calling FindFirstChild on nil or perhaps the instance is destroyed/gone. also
local frame = 1
while true do
if not script.Parent:FindFirstChild("Frame"..frame) then
script:Destroy()
end
if script.Parent:FindFirstChild("Frame"..frame) then
task.wait(script.Parent:FindFirstChild("Frame"..frame).TimeBetweenFrames.Value - 0.025)
game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(0.075, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = script.Parent:FindFirstChild("Frame"..frame).Value}):Play()
frame = frame + 1
end
task.wait(0.025)
end
you didn’t forget to do return after script:Destroy()???
(yes while loops run even after getting destroyed) (i knew this since 2022)
you could try a for loop instead so you don’t need to keep track of the ‘frames’ (:FindFirstChild("Frame"..frame))
local TS = game:GetService("TweenService")
local TInfo = TweenInfo.new(0.075, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
for _, CFrameValue in ipairs(script.Parent:GetChildren()) do
if CFrameValue:IsA("CFrameValue") then
local Tween = TS:GetService("TweenService"):Create(script.Parent, TInfo, {
CFrame = CFrameValue.Value
})
Tween:Play()
-- instead of having wait values for each tween, you could use 'Completed'
Tween.Completed:Wait()
end
task.wait(0.025)
end