Deck handler module script not functioning

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make this module script work, even though it’s not clear what the problem is. I’m making a card game that my family plays but in Roblox, and I need a module script to keep track of the 2 decks of cards that the game uses so that it doesn’t give out dupes of cards. The GiveCards event should give a cards back to the deck (like when you end a hand and return all of the played cards to the deck) and the TakeCards event should take them from the deck (like drawing from the deck).

  2. What is the issue? Include screenshots / videos if possible!
    This module doesn’t show any errors, but when I make another script require it, it says that the module has an error while loading.

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I can’t find anything that relates to my problem. It’s in ReplicatedStorage, which should be fine, but i moved it to some other places to troubleshoot. I have tried to put the module’s code into a normal script so that an error would fire, but nothing happened. I’m thinking it’s a syntax error since it’s while loading, but I’m not sure since I looked at the whole script and couldn’t find anything.

Here’s my code:

local rs = game:GetService("ReplicatedStorage")

local Deck = 
	{
		"AC", "AD", "AH", "AS",
		"2C", "2D", "2H", "2S",
		"3C", "3D", "3H", "3S",
		"4C", "4D", "4H", "4S",
		"5C", "5D", "5H", "5S",
		"6C", "6D", "6H", "6S",
		"7C", "7D", "7H", "7S",
		"8C", "8D", "8H", "8S",
		"9C", "9D", "9H", "9S",
		"0C", "0D", "0H", "0S",
		"JC", "JD", "JH", "JS",
		"QC", "QD", "QH", "QS",
		"KC", "KD", "KH", "KS",
		"!C", "!S",
		"AC", "AD", "AH", "AS",
		"2C", "2D", "2H", "2S",
		"3C", "3D", "3H", "3S",
		"4C", "4D", "4H", "4S",
		"5C", "5D", "5H", "5S",
		"6C", "6D", "6H", "6S",
		"7C", "7D", "7H", "7S",
		"8C", "8D", "8H", "8S",
		"9C", "9D", "9H", "9S",
		"0C", "0D", "0H", "0S",
		"JC", "JD", "JH", "JS",
		"QC", "QD", "QH", "QS",
		"KC", "KD", "KH", "KS",
		"!C", "!S"
	}
--give cards to the deck
rs.Events.GiveCards.OnServerEvent:Connect(function(plr, cards)
	if typeof(cards) ~= "table" then
		warn("sent a non-table value to the give cards event!")
	end
	for i, v in pairs(Deck) do
		if not table.find(cards, v) then
			table.insert(Deck, v)
			print("inserted "..tostring(v))
		else
			print("card already in deck, card is "..tostring(v))
		end
	end
end)

--take cards from the deck
rs.Events.TakeCards.OnServerEvent:Connect(function(plr, cards)
	if typeof(cards) ~= "table" then
		warn("sent a non-table value to the take cards event!")
	end
	for i, v in pairs(Deck) do
		if table.find(cards, v) then
			table.remove(Deck, table.find(cards, v))
			print("removed "..tostring(v))
		else
			print("card not found to remove, card is "..tostring(v))
		end
	end
end)

Deck.GetData = function()
	return Deck
end

return Deck

If you need more info, I am willing to do anything to fix this, although I dont know if anything else is relevant.

Do you happen to be requiring this ModuleScript from a LocalScript? If so, you’ll get a “Requested module experienced an error while loading” error because a LocalScript cannot use OnServerEvent. You could connect to your remote events in a Script that requires the ModuleScript, for example:

In ModuleScript:

local Deck = {}
local cardList = {
   --Put your array of strings here instead
}

function Deck.GiveCards(player, cards)
   --GiveCards logic here
end

function Deck.TakeCards(player, cards)
   --TakeCards logic here
end

--This function replaces `Deck.GetData = function()` from your code, as `Deck` is already returned by the require call to this module
function Deck.GetCardList()
   return cardList
end

return Deck

In Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Deck = require(PATH_TO_MODULE_SCRIPT)

ReplicatedStorage.Events.GiveCards.OnServerEvent:Connect(Deck.GiveCards)
ReplicatedStorage.Events.TakeCards.OnServerEvent:Connect(Deck.TakeCards)

Keep in mind that the data in Deck is not shared between server and clients. In other words, a completely different copy of the module Deck will exist for server, and for each player.

1 Like

Thanks so much! I didn’t know that the module acted as an extension of the local script requiring it, I thought it acted as part of the server.

A require’d ModuleScript operates in the same Lua environment as the LocalScript, Script, or ModuleScript that require’d it, so sometimes this is an extension of the client, sometimes of the server, and sometimes both client(s) and the server will require the same ModuleScript. Here’s a link to a tutorial that can explain better than I can: Intro to module scripts | Documentation - Roblox Creator Hub

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