Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ModuleScript, attempt to call a nil value? (help!)

Hey guys,

I'm making a card game on Roblox.

Currently, I have 2 modules and one server script.

ModuleScripts:

Deck

Deck = {}
Deck.__index = Deck

	function Deck.new(suit,rank)
		local newDeck ={}
		setmetatable(newDeck, Deck)
		
		newDeck.Suit = suit
		newDeck.Rank = rank
		
		return newDeck
	end

return Deck

Visualize

local Deck = require(game.ServerScriptService.Modules.Deck)

local Vis = {}
Vis.__index = Vis

	function Show()
		print((Deck.Rank).." of "..(Deck.Suit))
	end

return Vis

Server:

Deal

local Deck = require(game.ServerScriptService.Modules.Deck)
local Vis = require(game.ServerScriptService.Modules.Visualize)

newDeck = Deck.new("Diamonds",math.random(2,14))
Vis.Show()

All I'm trying to do is print the values of the new deck (Vis) created in (Deck). this works if I do everything in the server script, for some reason I'm getting the following error:

ServerScriptService.Deal:5: attempt to call a nil value

I feel like there's something super obvious I'm missing but I can't seem to put my finger on it..

Thanks in advance!

4 comments

0

Shouldn't it just be "Show()" instead of Vis.Show()?

0

Since the "Show()" function is located in the visualize ModuleScript, and I'm calling it from Deal, I have to locate it by Vis.Show()

0

You never bound the method to the Object: function Vis:Show().

0

local newDeck = setmetatable({}, Deck) - returns the metatable being set.

1 answer

✓ Accepted answer

The problem is that, because of Roblox's sandbox, your Show function is defined in the scope of the ModuleScript, so only functions within that script can access it. To do what you want, you'll have to define it within the Vis table by doing

function Vis.Show()
    print((Deck.Rank).." of "..(Deck.Suit))
end

Also, not sure if you're just doing this by preference or not, but you don't need to indent your functions like that. Perfectly fine if you like it better, but it may confuse some people.

This discussion is preserved as it was. New answers, comments, and votes are closed.