Why am I reciving this error in my OOP oriented script?

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local eventos = game.ReplicatedStorage.luftsMods.eventos

local rallySpaws = 15
local UisammountToPlaceRally = 3

local player = Players.LocalPlayer
local rallyFolder = workspace.rallyFolder

local rally = {}

local rallys = {}
rally.__index = rally

function rally.New(player)
	print("new")
	
	local self = {}
	self.Model = script.rally:Clone()
	self.Team = player.Team
	self.Spawns = rallySpaws
	
	
	self.Model:PivotTo(player.Character.HumanoidRootPart.CFrame)
	self.Model.Parent = rallyFolder
	self.Model.Name = player.Name .. "Rally"
	
	rallys[player.UserId] = self
	
	setmetatable(self, rally)	
	eventos.rallyPlace:FireAllClients(player.UserId, self)
	
	local rallah = rallys[player.UserId]
	rallah:PrintStats()
	
	return self
end


function rally.LeaderInit()
	
	local pressCounter = 0
	UIS.InputBegan:Connect(function(input, gp)
		if input.UserInputType == Enum.UserInputType.MouseButton3 and not gp then
			print(pressCounter)
			
			if pressCounter == UisammountToPlaceRally then
				pressCounter = 0
				eventos.rallyPlace:FireServer()
				print("fired")				
			else
				pressCounter += 1
			end
		end
	end)
	
end

function rally.PlayersInit()
	
	eventos.rallyPlace.OnClientEvent:Connect(function(player, self) --why does this NOT PRINT THE STATS
		print(player)
		print(self.Model)
		self:PrintStats()
	end)
	
end

function rally.ServerInit()
	
	eventos.rallyPlace.OnServerEvent:Connect(function(player)
		print("hered")
		rally.New(player)
	end)
	
end

function rally:PrintStats()
	print(self.Team, self.Model, self.Zestyness)
end

image
everything is called and I just don’t understand

4 Likes

did you use a . instead of a :?
Example on correct code:

local rally = rally.New(player)
rally:PrintStats()
3 Likes

the primary issue is my table getting it to the client.


function rally.PlayersInit()
	
	eventos.rallyPlace.OnClientEvent:Connect(function(player, rally) --why does this NOT PRINT THE STATS
		print(player)
		rally:PrintStats()
	end)
	
end

function rally.ServerInit()
	
	eventos.rallyPlace.OnServerEvent:Connect(function(player)
		local rally = rally.New(player)
		eventos.rallyPlace:FireAllClients(player.UserId, rally)
	end)
	
end

look at this for example, it will say that it doesn’t know what printstats is

1 Like

Maybe because self isn’t returning a metatable

1 Like

if you look on the output a serverscript will successfully print the variables

1 Like

ah, you have to use local self = setmetatable({}, rallys) instead of local self = {}

1 Like

Nevermind you have it, I’m kinda sleep deprived so sorry.

2 Likes

Look further down, I set it to a metastable I’m so lost

Edit
Here’s an example:

local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

local rally = {}
rally.__index = rally

function rally.new(player)
	local self = setmetatable({}, rally)

	self.player = player
	self.test = true

	return self
end

function rally.ClientInit()
	RemoteEvent.OnClientEvent:Connect(function(player, ...)
		local rally = rally.Deserialize(...)
		rally:PrintStats()
	end)
end

function rally.ServerInit()
	RemoteEvent.OnServerEvent:Connect(function(player)
		local rally = rally.new(player)
		local data = rally:Serialize()

		RemoteEvent:FireAllClients(player.UserId, data)
	end)
end

function rally.Deserialize(...)
	local player, test = unpack(...)

	local rally = rally.new(player)
	rally.test = test
	return rally
end

function rally:Serialize()
	return {self.player, self.test}
end

function rally:PrintStats()
	print(self.player, self.test)
end

return rally
function rally.PlayersInit()
	eventos.rallyPlace.OnClientEvent:Connect(function(player, object) 
		print(player)
		print(object.Model)
		object:PrintStats()
	end)
end

Assuming the second value in the parameter should be the object.

This should work, as previously you did self:PrintStats(), which wouldn’t work because :PrintStats() isn’t a method inside that object (in the .New() function).

(I’m also assuming that you have a remote that sends the new object into the remote.)