While coding a helper service for a game I am working on I have run into an invalid error with studio’s code analyzer. I am checking if a variable exists and if not setting it to true (as a default to true variable in a function) and the code analyzer flags it for “type mismatch: nil and boolean” which is improper because if I simply check for if the variable is false the nil AND manually specified false would satisfy the condition so I must check for either nil or boolean false.
The code in question is as follows:
ApolloIndex.PlayerAdded = function(self,func,RunOnChildren)
if not self or self ~= Apollo then
error("Attempting to index a method.")
end
if not func then
error("Missing parameter 1: Handler Function")
elseif typeof(func) ~= "function" then
error("Invalid parameter 1: Must be a function")
end
if typeof(RunOnChildren) == "nil" then
--error("Missing parameter 2: Run Function On Children")
RunOnChildren = true --Invalid type mismatch error, ignore studio's complaint here.
elseif typeof(RunOnChildren) ~= "boolean" then
error("Invalid parameter 2: Must be a boolean")
end
local ConnectionGUID = Apollo:GUID(false)
local Connection = Apollo:Connect(Apollo.Players.PlayerAdded,func,function() if PlayersAddedConnections[ConnectionGUID] then PlayersAddedConnections[ConnectionGUID] = nil end end)
PlayersAddedConnections[ConnectionGUID] = Connection
if RunOnChildren then
local Plrs = Apollo.Players:GetPlayers()
for i=1,#Plrs do
local succ,err = pcall(function() func(Plrs[i]) end)
if not succ then Connection:Disconnect() error(err) end
end
end
return Connection,ConnectionGUID
end
A screenshot of this error is here: https://photos.app.goo.gl/AZYfaDrqkahFDcVCA