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
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
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
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)
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