Unkown global self

Hello guys !

That’s probably a noob lua question, but all the answer I found didn’t make me understood why I got the error : “unknow global self” in the Health() and Stamina() functions.

--!nonstrict

local CommonStats = {}
CommonStats.__index = CommonStats

local function StatSetup(_typeName, _name, _character)
	local stat = Instance.new(_typeName)
	stat.Name = _name
	stat.Parent = _character.Stats
end

function CommonStats.new(_character : Model)
	local self = setmetatable({}, CommonStats)

    self.character = _character
	if _character:FindFirstChild("Stats") == nil then
        local stats = Instance.new("Folder")
        stats.Name = "Stats"
        stats.Parent = _character
    end

	self.Health = StatSetup("IntValue", "Health", _character)
	self.Stamina = StatSetup("IntValue", "Stamina", _character)
    return self
end

function CommonStats.Health()
    return self.character.Stats.Health
end

function CommonStats.Stamina()
    return self.character.Stats.Stamina
end
return CommonStats

Sorry and thanks :sweat_smile:

I think you just need to change the CommonStats.Health and CommonStats.Stamina functions to use : instead (function CommonStats:Health, function CommonStats:Stamina).

The colon is used for methods where you need to have self passed in; dot is more for functions, where you won’t get that. So in your case it’s not coming automatically.

The other issue I see is that this might have errors because you’re overwriting Health and Stamina - I think you may need to change those (the lines where you set self.Health = StatSetup and self.Stamina = StatSetup). But if it’s working right now then I might be reading that wrong.

I kinda want to slap my face x)
Thoses errors are so unprecise. Thanks !

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.