Help with FindFirstAncestor

I was trying to make a CollectionService system for one task that I’ll be using for a bunch of other stuff. They tweened fine and all, but there was no sound. I tried debugging it with prints but for some reason it just throws an error saying it doesn’t exist, which obviously does. I have no idea what to do beyond that because this is the only solution I know for this specific type of case. I know the syntax is correct because it doesn’t give any syntax errors. I’d appreciate any sort of help for me to understand what’s going on.

local function Execute(tar)
		local Open = tar:GetAttribute("OpenPos")
		local Closed = tar:GetAttribute("ClosedPos")

		local Sound = tar:FindFirstAncestor("Sound")
		print(Sound.Name) -- Throws out an error

		if tar:GetAttribute("Open") then
			TweenService:Create(tar, OCAnim, {Position = Closed}):Play()
			tar:SetAttribute("Open", false)
		else
			TweenService:Create(tar, OCAnim, {Position = Open}):Play()
			tar:SetAttribute("Open", true)
		end

		if Sound and Sound:GetAttribute("MovingOBJSFX") then
			Sound:Play()
		end
	end

The instance I highlighted is the target in question. (The script doesn’t exist as the child because it’s in ServerScriptService)
image

The issue is simple. You used the function wrong. Instance:FindFirstAncestor() is used when you are trying to find an object’s parent. In this context, LCurtain can’t use this function because it is not a descendant of the Sound object. They are objects having different parents, not an object as the parent of the other.

To fix this, just refer the Curtain folder instance and get the Sound object.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.