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

Trying to make a Gun Skin Crate opening system, Module Issue?

Prob 1 Module code did not return exactly one value Prob 2 Requested module experienced an error while loading

Script 1:

local Skinmodule = {}
Skinmodule.pets = {
	["Legendary"] = {
		game.ReplicatedStorage.Skins:WaitForChild("Why")

	};
	["Epic"] = {
		game.ReplicatedStorage.Skins:WaitForChild("Thin3")

	};
	["Rare"] = {
		game.ReplicatedStorage.Skins:WaitForChild("Thing2")

	};
	["Uncommon"] = {
		game.ReplicatedStorage.Skins:WaitForChild("Thing")
	}; 
	
	["Common"] = {
		game.ReplicatedStorage.Skins:WaitForChild("Wooden")
	};
}

Skinmodule.rarities = {
	["legendary"] = 5;
	
	["Epic"] = 10;
	
	["Rare"] = 15;
	
	["Uncommon"] = 30;
	
	["Common"] = 40;

	
}

Skinmodule.chooseRandomSkin = function()
	local counter = 0
	local randomNumber = math.random(1, 100)
	
	for rarity, weight in pairs(Skinmodule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then
			local rarityTable = Skinmodule.Skin[rarity]
			local chosenSkin = rarityTable[math.random{1, #rarityTable}]
		return Skinmodule
	end
	end
	end

	

Script 2

local SkinModule = require(game.ServerScriptService:WaitForChild("RarityScript"))

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	
	if player.leaderstats.Cash.Value >= 500 then
		
		local Skin = SkinModule.." selected"
		
		game.ReplicatedStorage.OpenCrate:FireClient(player,Skin)
	end
end)

1 comment

0

what what?

1 answer

All module scripts MUST return 1 value, Problem 1 is that you did not return anything, by your script 1 i see you want to return the Skinmodule so just put it into the last line:

return Skinmodule

And that was the reason why script 2 was erroring, this error means that module that you required has caused an error. The way how require works is that it reads all of the module code so if you for example do:

-- module script
print("Hello")

return true -- Module must return something

-- normal script
require(module script)

Output:
Hello

This won't cause problems while if you do

-- module script
error("Hello")

return true

-- normal script
require(module script)

Output:
-> Hello - StackBegin line 1
-> Requested module has errored!

This will break the script that required the module. Also you don't have to use WaitForChild on server and on ReplicatedStorage instances, if the module was created in the edit mode and is always in the explorer then it's not needed and only removes autocomplete.

1 comment

0

To elaborate on what @imKindra said, the way module scripts work is by having functions and variables in one place, and returning it so that other scripts can use this.When you put return SkinModule in that function, the script assumed that when you call that function, then return the variable SkinModule therefore causing your script to error.

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