Get rid of annoying linter warning

Hello,

my code warns with “Expected type table, got ‘TextBox | TextBotton | TextLabel’ instead” and I don’t know why. I’ve read a post saying that it’s because of there not having safeguards, but in this context it’s useless. Can anyone help me?

function translationEffect(Ui: TextLabel | TextBox | TextButton)
	local originalText = Ui.Text
	local stringLength, translatedStringLength = string.len(originalText), string.len(translation)
	while true do
		task.wait()
		Ui.Text = originalText
		for count = stringLength, 0, -1 do
			task.wait(.25)
			Ui.MaxVisibleGraphemes = count
		end
			Ui.Text = translation
		for count = 0, translatedStringLength, 1 do
			task.wait(.25)
			Ui.MaxVisibleGraphemes = count
		end
	end
end
2 Likes

Have you got

--!strict

or similar at the top of your script? If so, try removing it

If not - what is the line in question that is erroring

2 Likes

It’s not erroring, it’s the syntax linter that’s giving me a warning for seemingly no reason.

Can you show us where you call the function and what parameter you pass?

1 Like

(read comments)

local PlayersService = game:GetService("Players")
local LocalizationService = game:GetService("LocalizationService")
local success, translator = pcall(function() 
	local success, translator = pcall(LocalizationService:GetTranslatorForPlayerAsync(PlayersService.LocalPlayer))
	if not success then
		translator = LocalizationService:GetTranslatorForLocaleAsync(LocalizationService.SystemLocaleId)
	end
	return translator
end)



if success then
	if not game:IsLoaded() then
		game.Loaded:Wait()
	end
	local CollectionService = game:GetService("CollectionService")
	local TaggedInstances = CollectionService:GetTagged("TranslationEffect")
	local translation = translator:FormatByKey("build.hidden")

	function translationEffect(Ui: TextLabel | TextBox | TextButton) // Linter warning appears after adding ': TextLabel | TextBox | TextButton'
		local originalText = Ui.Text
		local stringLength, translatedStringLength = string.len(originalText), string.len(translation)

		while true do
			task.wait()
			
			--FIXME// Doesn't change anything. //FIXME--
			
			Ui.Text = originalText
			for count = stringLength, 0, -1 do
				task.wait(.25)
				Ui.MaxVisibleGraphemes = count
			end

			Ui.Text = translation

			for count = 0, translatedStringLength, 1 do
				task.wait(.25)
				Ui.MaxVisibleGraphemes = count
			end
		end
	end

	for _, Ui in TaggedInstances do
		task.spawn(translationEffect, Ui) // HERE TOO AAAAAAAA
	end
	
	CollectionService:GetInstanceAddedSignal("TranslationEffect"):Connect(function(Ui)
		task.spawn(translationEffect, Ui) // HERE HERE HERE COME HERE
	end)
else
	warn("Failed to load translator for player with error: "..translator)
end

Print what Ui is and its ClassName so we make sure the parameters are being passed correctly.

1 Like

It seems like my function doesn’t get executed no matter what I do.
EDIT: I misspelled the translationEffect tag.

Oh well, then that is probably the issue.

It prints TextLabel TextLabel - Client - TranslationEffect:21.
I really don’t think that it’s possible to diagnose linting issues this way.

I mean this by linting, by the way:
image

Hm, then it is a TextLabel. What happens if you remove the typing?

The warning disappears. Lorem ipsum,

Print the ClassName of the stuff passed to the function inside the function itself and not outside.

That is what I did. If it didn’t pass the arguments, the function wouldn’t work.

EDIT: My current script:

local PlayersService		=	game:GetService("Players")
local LocalizationService	=	game:GetService("LocalizationService")
local success, translator = pcall(function()
	return LocalizationService:GetTranslatorForPlayerAsync(PlayersService.LocalPlayer)
end)

if not success then
	error("TranslationEffect errored with: " .. tostring(translator))
end



--if not game:IsLoaded() then
--	game.Loaded:Wait()
--end
local CollectionService	=	game:GetService("CollectionService")
local TaggedInstances	=	CollectionService:GetTagged("translationEffect")
local translation		=	translator:FormatByKey("build.hidden")

function translationEffect(Ui: TextLabel | TextBox | TextButton)
	print(Ui, "\n" .. Ui.ClassName)
	local originalText = Ui.Text
	local stringLength, translatedStringLength = string.len(originalText), string.len(translation)

	while true do
		print(Ui, Ui.ClassName)
		task.wait()

		--TODO// add blinking "_" at the end of string //TODO--

		Ui.Text = originalText
		for count = stringLength, 0, -1 do
			task.wait(.15)
			Ui.MaxVisibleGraphemes	=	count
		end
		
		for _, , 1
		task.wait(5)
		
		Ui.Text = translation

		for count = 0, translatedStringLength, 1 do
			task.wait(.15)
			Ui.MaxVisibleGraphemes	=	count
		end
	end
end

CollectionService:GetInstanceAddedSignal("TranslationEffect"):Connect(function(Ui)
	task.spawn(translationEffect, Ui)
end)

for _, Ui in TaggedInstances do
	task.spawn(translationEffect, Ui)
end

Hm, then I don’t know what other solution there is aside from getting rid of the type checks.

1 Like

There’s no way to get rid of the warning. Luau can’t simplify or understand which type to use when indexing, so it throws that warning when it can’t figure it out. You can do this with regular tables as well and you’ll note the same warning, it’s an unfortunate downside to using Type Unions. Hopefully by Summer, the Luau team will finish their new type solver which should solve 99% of the problems that Luau has now.

What I would recommend is to simply recast the same UI variable as just a TextLabel. At least, if the “Ui” variable being typed is super important:

function translationEffect(Ui: TextLabel | TextBox | TextButton)
	local Ui = (Ui :: TextLabel)
2 Likes

This works, but I don’t understand: what’s type casting? I’ve searched on luau-lang.org but I really don’t understand what it does.

It basically forces the type to be something else.

In this case, we are overriding the Ui argument variable with a variable of the same name (thus overriding it) and casting the type to be a TextLabel, or in other words, forcing the type of Ui to be a TextLabel.

There are some cases where it’ll throw an error and tell you that casting the type isn’t possible, so in some cases you may need to do:

local Ui: TextLabel = (Ui :: any)

but in this case you don’t since TextLabel is already a possible option

1 Like

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