Attempt to call missing method of table

Would like a fairly quick response, as this is breaking my game and people are sending many bug reports:

local f = {}

local runService = game:GetService("RunService")

local tweenService = game:GetService("TweenService")

game.Players.PlayerAdded:Connect(function(player)
	local baseFlicker = player:WaitForChild("PlayerGui"):WaitForChild("MainGui"):WaitForChild("AnimatronicFlicker")

	--[[local SoftFlickering = false
	local HardFlickering = false
	local CurrentlyOn = false
	]]
	
	local tableOfFlickers = {}
	
	function f:Flicker(animatronic: string)
		tableOfFlickers[animatronic] = {Flicker = baseFlicker:Clone(), minTransparency = 500, maxTransparency = 700}
		local t = tableOfFlickers[animatronic]
		t.Flicker.Parent = player.PlayerGui.MainGui
		t.Flicker.Name = animatronic
		
		local frames = 0
		t.Connection = runService.Heartbeat:Connect(function(delta)
			frames += delta
			if frames >= 1/60 then
				frames = 0
				t.Flicker.ImageTransparency = math.random(t.minTransparency,t.maxTransparency)/1000
			end
		end)
	end

	function f:HardFlicker(animatronic: string)
		local t = tableOfFlickers[animatronic]
		t.minTransparency = 150
		t.maxTransparency = 350
	end
	
	function f:SetCustomMinAndMaxTransparency(animatronic: string, min: number, max: number)
		local t = tableOfFlickers[animatronic]
		t.minTransparency = min
		t.maxTransparency = max
	end

	function f:StopStatic(animatronic: string)
		local t = tableOfFlickers[animatronic]
		t.Connection:Disconnect()
		local In = tweenService:Create(t.Flicker, TweenInfo.new(0.01,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0),{ImageTransparency = 0})
		In:Play()
		In.Completed:Wait()
		local out = tweenService:Create(t.Flicker, TweenInfo.new(0.08,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0),{ImageTransparency = 1})
		out:Play()
		out.Completed:Wait()
		t.Flicker.Visible = false
		t.Flicker:Destroy()
		table.remove(tableOfFlickers,table.find(tableOfFlickers,t))
	end
	
	function f:HasStatic(animatronic: string)
		if tableOfFlickers[animatronic] ~= nil then
			return true
		else
			return false
		end
	end
end)

return f

This is my module script, when I try to use any kind of method, such as :Flicker or :HasStatic, it gives me the error message: Attempt to call missing method of table.

Please help.

1 Like

To confirm - is this module script on the client side or server side?

It can be called from either, but more than likely the server. (in this context it’s the server)

I suspect the issue is that at the time when you are trying to call the methods, it hasn’t actually defined them (as PlayerAdded hasn’t fired).

By saying it’s been called on the server – is it only doing something to affect the UI of the players?

Yes, so the animatronic calls it because it needs to make the screen flicker, and I coded the animatronics on the server side. I suspect it’s a roblox bug because it was just working a couple hours ago. I have made 0 changes to it. It broke out of nowhere. I also tried reverting my place to an old save, and it still did nothing.

1 Like

You should probably try to restructure the code so that the methods are not within a player added event. I don’t think its doing you any good.

I would recommend making it only client sided. In doing that, you should get rid of the PlayerAdded bit and instead define player as game.Players.LocalPlayer.

Then, when the server wants the player’s UI to change, you should use a RemoteEvent to tell the client to call a particular function

It’s a 1 player server and I need it to access the player.

That’s not necessary though, I just don’t know why my code broke out of nowhere. I didn’t change it all. It’s been working for the longest time.

And if you don’t want to switch it over to client side, I would recommend still getting rid of the playeradded (given it’s a one player server), and instead add this line in place of the PlayerAdded connection:

local player = game.Players:GetPlayers()[1] or game.Players.PlayerAdded:Wait()

this will ensure the player exists

edit: well, looks like this reply was completely ignored, even though it contained the solution..

Remember to define .__index
(Nevermind this is only done when working with metatables sorry)

local f = {}
f.__index = f

local runService = game:GetService("RunService")

local tweenService = game:GetService("TweenService")

game.Players.PlayerAdded:Connect(function(player)
	local baseFlicker = player:WaitForChild("PlayerGui"):WaitForChild("MainGui"):WaitForChild("AnimatronicFlicker")

	--[[local SoftFlickering = false
	local HardFlickering = false
	local CurrentlyOn = false
	]]
	
	local tableOfFlickers = {}
	
	function f:Flicker(animatronic: string)
		tableOfFlickers[animatronic] = {Flicker = baseFlicker:Clone(), minTransparency = 500, maxTransparency = 700}
		local t = tableOfFlickers[animatronic]
		t.Flicker.Parent = player.PlayerGui.MainGui
		t.Flicker.Name = animatronic
		
		local frames = 0
		t.Connection = runService.Heartbeat:Connect(function(delta)
			frames += delta
			if frames >= 1/60 then
				frames = 0
				t.Flicker.ImageTransparency = math.random(t.minTransparency,t.maxTransparency)/1000
			end
		end)
	end

	function f:HardFlicker(animatronic: string)
		local t = tableOfFlickers[animatronic]
		t.minTransparency = 150
		t.maxTransparency = 350
	end
	
	function f:SetCustomMinAndMaxTransparency(animatronic: string, min: number, max: number)
		local t = tableOfFlickers[animatronic]
		t.minTransparency = min
		t.maxTransparency = max
	end

	function f:StopStatic(animatronic: string)
		local t = tableOfFlickers[animatronic]
		t.Connection:Disconnect()
		local In = tweenService:Create(t.Flicker, TweenInfo.new(0.01,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0),{ImageTransparency = 0})
		In:Play()
		In.Completed:Wait()
		local out = tweenService:Create(t.Flicker, TweenInfo.new(0.08,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0),{ImageTransparency = 1})
		out:Play()
		out.Completed:Wait()
		t.Flicker.Visible = false
		t.Flicker:Destroy()
		table.remove(tableOfFlickers,table.find(tableOfFlickers,t))
	end
	
	function f:HasStatic(animatronic: string)
		if tableOfFlickers[animatronic] ~= nil then
			return true
		else
			return false
		end
	end
end)

return f

This is the first time I see code defining functions inside an event of something that is outside it, no wonder that you get weird errors while doing it.

The real issue here is that your module is missing a parameter/argument that you try to take from the event, the player argument. Try to move your code outside the event and add a player parameter to the functions, then inside the event call the proper functions and pass the player object to them. See if the error still continues.

I just noticed you also define a table that is per player, you can either make that global with player instances as indexes or convert flicker to an OOP object and construct a different one for each player, each with a different copy of tableOfFlickers.

1 Like

Not completely right, but what I did was get rid of the event and define the player a different way. I think the issue was that the player would join faster than ModuleScript would get required. So it’s waiting and waiting for the player to join to add the methods. Partial solution (your solution would’ve worked as well though)