Typechecking issue

I’m working on a function and I ran into a typechecking issue that I’m unsure of. This error is occurring on the first line within the function:

Type 'Instance' could not be converted into 'ProximityPrompt?'

The Luau website on this says this though:

When one type inherits from another type, the type checker models this relationship and allows to cast a subclass to the parent class implicitly, so you can pass a Part to a function that expects an Instance.

This quote doesn’t exactly relate to the issue here, but why can’t I define prompt as a ProximityPrompt which inherits from Instance. Do I have to define prompt as an Instance to get this to work? Here’s the function:

--!strict

function Prompts:InvertPromptAction(parent: Instance, action1: string, action2: string)
	local prompt: ProximityPrompt? = parent:FindFirstChild(action1) or parent:FindFirstChild(action2)

	if prompt then
		if prompt.Name == action1 then
			Prompts:ChangeAction(prompt, action2)
		else
			Prompts:ChangeAction(prompt, action1)
		end
	end
end
2 Likes

Same here, none of these works:

I am also confused as to why when SurfaceGui sure is an Instance.

why the necropost?
also, GetChildren returns { Instance } and you’re trying to type it as SurfaceGui, which it isn’t

for _, Display in Panel:GetChildren() do -- pairs is not needed anymore, Display will automatically be inferred as Instance
    if not Display:IsA("SurfaceGui") then continue end -- this will refine Display's type as a SurfaceGui
    -- code
end

please make a topic of your own next time

2 Likes