OOP doesn't pass the functions?

I tried many things, but the functions never pass through and it always ends up erroring with attempt to call missing method 'End' of table or attempt to call missing method 'OnSpawn' of table Here’s the code:

TDM = {}
TDM.__Index = TDM

function TDM.new()
	local self = {
		Ended = false,
		Blue = nil,
		Red = nil,
		RoundValues = nil,
		Started = tick()
	}
	setmetatable(self, TDM)
	print(self, TDM)
	
	local Blue = Instance.new("Team")
	Blue.TeamColor = BrickColor.new("Really blue")
	Blue.Name = "Blue"
	Blue.AutoAssignable = false
	Blue.Parent = game.Teams
	
	local Red = Instance.new("Team")
	Red.TeamColor = BrickColor.new("Really red")
	Red.Name = "Red"
	Red.AutoAssignable = false
	Red.Parent = game.Teams
	
	self.Blue = Blue
	self.Red = Red
	
	local RoundValues
	
	if not game.ReplicatedStorage:FindFirstChild("RoundValues") then
		RoundValues = Instance.new("Folder")
		RoundValues.Name = "RoundValues"
		RoundValues.Parent = game.ReplicatedStorage
	else
		RoundValues:ClearAllChildren()
	end
	
	local gamemode = Instance.new("StringValue")
	gamemode.Name = "Gamemode"
	gamemode.Value = "Team Deathmatch"
	gamemode.Parent = RoundValues
	
	local BlueKills = Instance.new("NumberValue")
	BlueKills.Name = "BlueKills"
	BlueKills.Parent = RoundValues
	
	local RedKills = Instance.new("NumberValue")
	RedKills.Name = "RedKills"
	RedKills.Parent = RoundValues
	
	local TimeLeft = Instance.new("NumberValue")
	TimeLeft.Name = "TimeLeft"
	TimeLeft.Value = 600
	TimeLeft.Parent = RoundValues
	
	self.RoundValues = RoundValues
	
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("Stats") then
			v.Stats.RoundStats.Kills.Value = 0
			v.Stats.RoundStats.Deaths.Value = 0
		end
	end
	
	self.Started = tick()
	
	task.spawn(function()
		while not self.Ended do
			task.wait(1)
			TimeLeft.Value = 600-(math.floor(tick()-self.Started))
			if math.floor(tick() - self.Started) >= 600 then
				self:End()
				break
			end
		end
	end)
	
	return self
end

function TDM:OnKill(killer: Player)
	if self.RoundValues.RedKills.Value >= 100 or self.RoundValues.BlueKills.Value >= 100 then
		return
	end
	
	local team = killer.Team.Name
	self.RoundValues:FindFirstChild(team.."Kills").Value += 1
	
	if self.RoundValues:FindFirstChild(team.."Kills").Value >= 100 then
		self:End(team)
	end
end

function TDM:OnSpawn(plr: Player)
	if not plr.Team then
		if #self.Blue:GetPlayers() > #self.Red:GetPlayers() then
			plr.Team = self.Red
		elseif #self.Blue:GetPlayers() < #self.Red:GetPlayers() then
			plr.Team = self.Blue
		else
			plr.Team = math.random(2) == 1 and self.Blue or self.Red
		end
	end
end

function TDM:End(winner)
	self.Ended = true
	local winners = self[winner]:GetPlayers()
	game.ReplicatedStorage.RoundEnd:FireAllClients("RoundEnd", {players = winners, team = self[winner]})
	task.wait(15)
	game.ReplicatedStorage.RoundEnd:FireAllClients("NextRound")
	self.RoundValues:Destroy()
	self.Blue.Team:Destroy()
	self.Red.Team:Destroy()
end

return TDM
1 Like

Can you show us the code that’s using the object (where the attempt to call missing method 'OnSpawn' error is occurring)?

Yeah, sure. Here it is:

function handleplr(plr: Player)
	local plrstats = Instance.new("Folder")
	plrstats.Name = "Stats"
	plrstats.Parent = plr

	local savedstats = mainds:GetAsync(plr.UserId) or {}

	local kills = Instance.new("NumberValue")
	kills.Name = "Kills"
	kills.Value = savedstats.Kills or 0
	kills.Parent = plrstats

	local deaths = Instance.new("NumberValue")
	deaths.Name = "Deaths"
	deaths.Value = savedstats.Deaths or 0
	deaths.Parent = plrstats

	local cash = Instance.new("NumberValue")
	cash.Name = "Cash"
	cash.Value = savedstats.Cash or 0
	cash.Parent = plrstats

	local pt = Instance.new("IntValue")
	pt.Name = "Play Time"
	pt.Parent = plr
	pt.Value = savedstats.Time or 0

	local equipped = Instance.new("Folder")
	equipped.Name = "EquippedGuns"
	equipped.Parent = plr

	local roundstats = Instance.new("Folder")
	roundstats.Parent = plrstats
	roundstats.Name = "RoundStats"

	local roundk = Instance.new("NumberValue")
	roundk.Name = "Kills"
	roundk.Value = 0
	roundk.Parent = roundstats

	local roundd = Instance.new("NumberValue")
	roundd.Name = "Deaths"
	roundd.Value = 0
	roundd.Parent = roundstats

	if savedstats.Guns then
		for i,v in pairs(savedstats.Guns) do
			if not table.find(allguns, v) then
				table.remove(savedstats.Guns, i)
			end
		end
	end

	if savedstats.Equipped then
		if not table.find(savedstats.Guns, savedstats.Equipped.Primary) then savedstats.Equipped.Primary = "" end
		if not table.find(savedstats.Guns, savedstats.Equipped.Secondary) then savedstats.Equipped.Secondary = "" end
	end

	local primary = Instance.new("StringValue")
	primary.Name = "Primary"
	primary.Value = savedstats.Equipped and savedstats.Equipped.Primary or "AKM"
	primary.Parent = equipped

	local secondary = Instance.new("StringValue")
	secondary.Name = "Secondary"
	secondary.Value = savedstats.Equipped and savedstats.Equipped.Secondary or "Pistol"
	secondary.Parent = equipped

	local guns = savedstats.Guns or {}

	for i,v in pairs(gunprices) do
		if v == 0 then
			if not table.find(guns, i) then
				table.insert(guns, i)
			end
		end
	end

	plrgundata[plr.UserId] = guns

	task.spawn(function()
		while task.wait(1) do
			plr["Play Time"].Value += 1
		end
	end)

	plr.CharacterAdded:Connect(function(char)
		if gamemode then gamemode:OnSpawn(plr) end
		
		local hum: Humanoid = char:WaitForChild("Humanoid")
		hum.Died:Connect(function()
			game.ReplicatedStorage.PlayerDied:FireClient(plr)
			char:Destroy()
		end)

		local prim = plr.EquippedGuns.Primary.Value
		prim = game.ReplicatedStorage.Guns[prim]:Clone()
		local sec = plr.EquippedGuns.Secondary.Value
		sec = game.ReplicatedStorage.Guns[sec]:Clone()

		prim.Parent = plr.Backpack
		sec.Parent = plr.Backpack

		task.wait(.5)
		if char:FindFirstChildOfClass("ForceField") then
			char:FindFirstChildOfClass("ForceField"):Destroy()
		end
	end)
end

That’s the on spawn in the character added function (at the bottom). Also, the End is actually here:

1 Like

You misspelled __index. It needs to be a lowercase i.

3 Likes

I did declare end at the end of the module.

2 Likes

Wow, that was such a simple mistake, thank you! I was stuck on that for a couple hours wondering why it wasn’t working lol

2 Likes

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