so I have a module script that returns a player and playerCharacter within a callback function
Module script :
if canLoop == true then
playerObject.CharacterAdded:Connect(function(modelObject:Model)
callback(playerObject, modelObject)
end)
else
playerObject.CharacterAdded:Once(function(modelObject:Model)
callback(playerObject, modelObject)
end)
end
in the serverScript I get this error
yes i’ve looked around for a problem like this and none of them show a callback function so this is new and I’m using --!strict I don’t think this is a bug but if it is let me know
and also yes I’ve used all the other ways to see if I can bypass this error none worked the error part I believe is this
But, --!strict is not real strict typing and I’ve had some odd problems with it (crap like type 'string' could not be converted into 'string | string | number | buffer | thread | bool | ....' and it just kept going). In my opinion, if you want strict typing use roblox-ts. But, if you want to stick to --!strict and Luau, use the :: operator to cast types to other types. For instance, x :: any and it will work. I don’t recommend it as a long-term solution, but should be enough to hold you over until someone who has more experience wrangling --!strict can help.
this is the entire module side that gets this error
function module:setPlayerHook(canLoop:boolean, callback:(Player, Model) -> (Player, Model))
if not canLoop or not callback then
warn("nil passed returning.")
return
else
if typeof(canLoop) ~= "boolean" or typeof(callback) ~= "function" then
warn("wrong type")
return
else
playerService.PlayerAdded:Connect(function(playerObject:Player)
if canLoop == true then
playerObject.CharacterAdded:Connect(function(modelObject:Model)
callback(playerObject, modelObject)
end)
else
playerObject.CharacterAdded:Once(function(modelObject:Model)
callback(playerObject, modelObject)
end)
end
end)
end
end
end
Not sure then. Like I said, --!strict is a trap in Luau, if you want strict typing you should try out roblox-ts. I’ve had too many problems with --!strict to trust it with anything.
I don’t understand this. Do you mean --!native? If so, I haven’t used it too much so you could be correct.
Sure, it’s slower than Luau because it compiles to Luau, but OP still gets the strict typing he wants, plus a far better type system. I don’t want to argue with you as to not clutter the thread.
Why do you declear function with : ?
Well obviously it will not work
its equialent to module.setPlayerHook(self,canLoop:boolean, callback:(Player, Model) → (Player, Model))
Essentially you create “invisible” argument self that you can clearly see when calling that
Yes, that’s my point. I’ve gotten “this one error” too many times. I’ve just given up on --!strict and if I want strict typing I’ll use TypeScript. I don’t use it for performance-critical code, though.
i fixed your code and now it should work:
Dont declear functions with “:” instead of “.” if you are not using argument self; and even if you do i still would avoid doing that
local Players = game:GetService("Players")
local module = {}
function module.setPlayerHook(canLoop:boolean, callback:(Player, Model) -> (Player, Model))
if not canLoop or not callback then
warn("nil passed returning.")
return
else
if typeof(canLoop) ~= "boolean" or typeof(callback) ~= "function" then
warn("wrong type")
return
else
Players.PlayerAdded:Connect(function(playerObject:Player)
if canLoop == true then
playerObject.CharacterAdded:Connect(function(modelObject:Model)
callback(playerObject, modelObject)
end)
else
playerObject.CharacterAdded:Once(function(modelObject:Model)
callback(playerObject, modelObject)
end)
end
end)
end
end
end
Still giving the type pack error and reason I had module: is because I heard you add : for if it takes values and . if it doesn’t but all my functions usually take a value
no.
That not how things work.
If you do get confused with metatable OOP you can use alternative (that would be better in your case)
Just dont use : when declearing function becouse it creates unneeded (in your case) argument.
Could you please show where you call this function?
That could be the reason as to why it errors
I’ve used OOP before and I just heard you use : for values and . for none ones I changed it and now they use . but let me change something its a bit of a mess now
function module:myFunc()
print(self) -- This variable here is implicitly defined when using :
end
module.myFunc() -- Does not work because you aren't providing self
module:myFunc() -- Should, because you are implicitly providing self