How to get rid of these warnings in strict mode?

So I just started Object Oriented Programming but I run to this annoying problem. I keep getting warnings saying that setmetatable should take a table and that animationTracks cannot be added to a table.

--!strict
local Creature = require(script.Parent)

local Enemy = {}
Enemy.__index = Enemy
setmetatable(Enemy, Creature)

function Enemy.new(model: Model)
	local newEnemy = Creature.new(model)
	setmetatable(newEnemy, Enemy)
	
	newEnemy.animationTracks = {}

	return newEnemy
end

return Enemy
I get the following warnings

image

setmetatable should take a table


cannot add property "animationTrack" to table

How can I get rid of them? Any workaround or other solution that would work?

Shot in the dark but it might be because you don’t have a type for creature? It should go away once you assert that it’s a table.

This doesn’t give me any typecheck errors:

--!strict

type Creature = {
	animator: Animator;
	config: Configuration;
	model: Model;
	root: BasePart
}

local creature = Creature.new() :: Creature

setmetatable(creature, {})
1 Like

Thanks, it works. What about this one, it’s really confusing and makes no sense to me.

--!strict

local Movement = {}
Movement.__index = Movement

function Movement.new(model: Model, path: Folder)
	local newMovement = {}
	setmetatable(newMovement, Movement)
    -- ...
    return newMovement
end

function Movement:Start()
    -- ...
end

return Movement
local movement = Movement.new(model, path)
movement:Start()

This does not only happen to Start(), it happens to all functions really.

I see.

You can try using assertion to assert that movement is of proper type.

type movement = {
    alignPosition: AlignPosition;
    lastSpeed: number;
    -- etc
}

local movement = Movement.new(model, path) :: movement -- use 2 colons to assert that it's of proper type
movement:Start()
1 Like

Now the last question, I get this really awful warning.

image

and it only pops up as soon as I add child.Value even with :: number it will not go away.

task.delay(child.Value :: number, function()

end)

image

I think it has to do with your assertion.

Assert that child is a Number/IntValue and you should be able to remove the child.Value :: number assertion.

image

It makes no difference.

It works with number but not my child.Value

image

Are you asserting child is an intValue? You could also make another variable which is kinda redundant but it should work.

local number = child.Value :: number
task.delay(number, function() --...

It worked only after I made two variables and asserted them.

local freezeInfo = child :: NumberValue
local freezeTime = freezeInfo.Value :: number
task.delay(freezeTime, function()

end)
1 Like