#Why does the type engine (the in beta one) turn a Folder type into a Table type after a IF

1 messages · Page 1 of 1 (latest)

long plinth
#

so i have this code

cosmeticDef.RMTPlayerWantsToBuyWithCash.OnServerEvent:Connect(function(player: Player, ...: any) 
        --not relevant logic
        if canBuy then
            cosmeticDef.NUMStock.Value -= 1
            if cosmeticDef.NUMStock.Value == 0 then
                cosmeticDef.BOOLIsForSale.Value = false
            end
            local newCosmeticDefForPlayer = module.createCosmeticDef(cosmeticDef)
            newCosmeticDefForPlayer.OBJPlayerWhoOwnsThis.Value = player
            newCosmeticDefForPlayer.OBJCharacterWearingThis.Value = playerSheep
        end    
    end)

and in the piece

if cosmeticDef.NUMStock.Value == 0 then
                cosmeticDef.BOOLIsForSale.Value = false
            end

the type checker trows the error

Type Error: (120,5) Key 'BOOLIsForSale' not found in table '{ read NUMStock: { read Value: unknown } }'

and...... why, i dont understand why is it converting the type i declared export type _cosmeticDef = typeof(script.CosmeticDef) into { read NUMStock: { read Value: unknown } }

i tested a bunch of stuff, nothing worked. the { read NUMStock: { read Value: unknown } } seems to be directly correlated to what ever the if is evaluating, so if it add if cosmeticDef.RandomStuff.Value == "acacaca" and cosmeticDef.skibidySigma.Value == "amongus" the table will expand and show them

can someone explain to me ### is happening? T_T

languid yacht
#

Lemmme help

long plinth
#

Why does the type engine (the in beta one) turn a Folder type into a Table type after a IF

long plinth
languid yacht
#

local module = require(script.Parent.Module) -- or wherever your module is
type _cosmeticDef = typeof(script.CosmeticDef)

cosmeticDef.RMTPlayerWantsToBuyWithCash.OnServerEvent:Connect(function(player: Player, ...: any)
-- Explicit type annotation for cosmeticDef
local cosmeticDef: _cosmeticDef = script.CosmeticDef

--not relevant logic
if canBuy then
    cosmeticDef.NUMStock.Value -= 1

    if cosmeticDef.NUMStock.Value == 0 then
        cosmeticDef.BOOLIsForSale.Value = false
    end

    local newCosmeticDefForPlayer = module.createCosmeticDef(cosmeticDef)
    newCosmeticDefForPlayer.OBJPlayerWhoOwnsThis.Value = player
    newCosmeticDefForPlayer.OBJCharacterWearingThis.Value = playerSheep
end

end)

languid yacht
languid yacht
long plinth
#

also it does´nt work

languid yacht
long plinth
#

the local cosmeticDef: _cosmeticDef = script.CosmeticDef need to be after the if

#

but thats besides the point, i dont want to have to slap that after every if

languid yacht
#

local module = require(script.Parent.Module)

-- Define the structure of your CosmeticDef object
type CosmeticDef = {
NUMStock: IntValue,
BOOLIsForSale: BoolValue,
OBJPlayerWhoOwnsThis: ObjectValue,
OBJCharacterWearingThis: ObjectValue,
RMTPlayerWantsToBuyWithCash: RemoteEvent,
}

-- Get the cosmetic definition and cast it to the correct type
local cosmeticDef = script.CosmeticDef :: CosmeticDef

cosmeticDef.RMTPlayerWantsToBuyWithCash.OnServerEvent:Connect(function(player: Player, ...: any)
-- Your custom logic to check if the player can buy
if canBuy then
cosmeticDef.NUMStock.Value -= 1

    if cosmeticDef.NUMStock.Value == 0 then
        cosmeticDef.BOOLIsForSale.Value = false
    end

    local newCosmeticDefForPlayer = module.createCosmeticDef(cosmeticDef)
    newCosmeticDefForPlayer.OBJPlayerWhoOwnsThis.Value = player
    newCosmeticDefForPlayer.OBJCharacterWearingThis.Value = playerSheep
end

end)

languid yacht
#

Lemme tell you how to use it

#

Replace canBuy and playerSheep with your actual logic or variables.
Make sure CosmeticDef under script has those children:
NUMStock (IntValue), BOOLIsForSale (BoolValue), RMTPlayerWantsToBuyWithCash (RemoteEvent), OBJPlayerWhoOwnsThis (ObjectValue), OBJCharacterWearingThis (ObjectValue)

#

Follow the steps

long plinth
languid yacht
#

GoodBye

long plinth
# languid yacht GoodBye

bye?, its a typechecker issue and questions, the children already exist, the code works, it does´nt trow an error, i want to know why is the typechecker doing that and if there is a better solution than just placing cosmeticDef:_cosmeticDef = cosmeticDef every 3 lines of code

long plinth
wanton kestrel
long plinth
# wanton kestrel can you show the "export type" part?
local cosmeticDefsFolder = workspace.CosmeticDefs
local runService = game:GetService("RunService")
local uis = game:GetService("UserInputService")

type _sheep = typeof(game.StarterPlayer.StarterCharacter)
type _statsFolder = typeof(game.StarterPlayer.StarterCharacter.FOLDStats)

--export type _cosmeticDef = index<typeof(script.CosmeticDef)>
--export type _cosmeticDef = keyof<typeof(script.CosmeticDef:GetChildren())>
export type _cosmeticDef = typeof(script.CosmeticDef)
--export type _cosmeticDef = index<typeof(script.CosmeticDef), keyof<typeof(script.CosmeticDef)>>
wanton kestrel
long plinth
wanton kestrel
long plinth
#

its pretty long actually

wanton kestrel
#

ohh ok got it

#

lemme think about it a sec

long plinth
wanton kestrel
#

weeeeeeeeelp okay soo basically it doesn't think "BOOLIsForSale" exists inside cosmeticDef... uhhhhh

#

this is painful

#

or it might be NUMStock that isn't considered a number

wanton kestrel
long plinth
long plinth
wanton kestrel
#

😭
not sure it works, but try:
if (cosmeticDef.NUMStock.Value :: number) == 0 then

wanton kestrel
#

or just sadly accept it... sometimes (rarely) there just isn't a fix to these typed crap, lua was made to be dynamic, luau forced it to be typed, it's just a strong contrast

#

or, what is waaaay more common and done in lua via visual studio extensions, add comments!
just like "-- crap luau thinks its unknown but it actually is a number"

wanton kestrel
#

didnt have any clue if it worked on ifs aswell, apparently no

long plinth
wanton kestrel
wanton kestrel
#

try posting in roblox dev forum, there's more people who plan on hurting themselves on this