Dictionary not being returned from module script

image
I’m making a module that would choose a random dictionary from either bad events or good events, however, it will not show me the Display key that each dictionary has.

This is my full ModuleScript:

local GameFunc = {}

local GoodChoices = {
	--// Health //--
	["IncreaseHP"] = {
		Display = "Gain %d max HP.", --> Format with amount of HP gained. (Randomized)
		Rarity = 1 
	},

	--// DAMAGE //--
	["IncreaseDMG"] = {
		Display = "Deal %d more damage.",
		Rarity = 1 
	},

	--// JUMPHEIGHT //--
	["IncreaseJP"] = {
		Display = "Jump %d studs higher.",
		Rarity = 1 
	},

	--// WALKSPEED //--
	["IncreaseWS"] = {
		Display = "Walk %d studs/s faster.",
		Rarity = 1 
	},
}
local BadChoices = {
	["DecreaseHP"] = {
		Display = "Lose %d max HP", --> Format with amount of HP lost. (Randomized)
		Rarity = 1 
	},
	["DecreaseDMG"] = {
		Display = "Gain %d more damage.",
		Rarity = 1 
	},
	["DecreaseJP"] = {
		Display = "Jump %d studs lower.",
		Rarity = 1 
	},
	["DecreaseWS"] = {
		Display = "Walk %d studs/s slower.",
		Rarity = 1 
	},
}

GameFunc.GoodChoices = GoodChoices
GameFunc.BadChoices = BadChoices

--//// RARITY / WEIGHT CALCULATIONS \\\\--
function GameFunc:SelectChoice(choices)
	local TotalWeight = 0
	for _, rarity in pairs(choices) do
		TotalWeight = TotalWeight + (1 / rarity.Rarity)
	end
	
	local RandomWeight = math.random() * TotalWeight
	local CumulativeWeight = 0
	
	for INDEX, rarity in pairs(choices) do
		CumulativeWeight = CumulativeWeight + (1 / rarity.Rarity)
		if RandomWeight <= CumulativeWeight then
			return INDEX, choices[INDEX]
		end
	end
end

return GameFunc

As seen in the image above, Display is for some reason considered [“%ERROR-ID%”]. Why’s that? I believe it is preventing me from accessing the string.

Your script seems to be working right, there is not issues in it.

I guess the problem is that you missed something when calling :SelectChoice() function.
It should look like this:

local ms = require(script.Parent.ModuleScript) -- your module script
local Index, ChosenTable = ms:SelectChoice(ms.BadChoices) -- don't forget to use `:` instead of `.` and dont forget to provide table with choices as an argument
print(Index, ChosenTable) -- results

Well, at least in completely new and clean place there are no such problem with your script

1 Like

You can create a custom type that clarifies the contents of the tables, and export the type so that the script that requires the module is also able to understand the contents:

export type data = {
	Display: string,
	Rarity: number}

export type GoodChoices = {
	IncreaseHP: data,
	IncreaseDMG: data,
	IncreaseJP: data,
	IncreaseWS: data}

export type BadChoices = {
	DecreaseHP: data,
	DecreaseDMG: data,
	DecreaseJP: data,
	DecreaseWS: data}

local GameFunc = {}

local GoodChoices: GoodChoices = {
	--// Health //--
	["IncreaseHP"] = {
		Display = "Gain %d max HP.", --> Format with amount of HP gained. (Randomized)
		Rarity = 1 
	},

	--// DAMAGE //--
	["IncreaseDMG"] = {
		Display = "Deal %d more damage.",
		Rarity = 1 
	},

	--// JUMPHEIGHT //--
	["IncreaseJP"] = {
		Display = "Jump %d studs higher.",
		Rarity = 1 
	},

	--// WALKSPEED //--
	["IncreaseWS"] = {
		Display = "Walk %d studs/s faster.",
		Rarity = 1 
	},
}
local BadChoices: BadChoices = {
	["DecreaseHP"] = {
		Display = "Lose %d max HP", --> Format with amount of HP lost. (Randomized)
		Rarity = 1 
	},
	["DecreaseDMG"] = {
		Display = "Gain %d more damage.",
		Rarity = 1 
	},
	["DecreaseJP"] = {
		Display = "Jump %d studs lower.",
		Rarity = 1 
	},
	["DecreaseWS"] = {
		Display = "Walk %d studs/s slower.",
		Rarity = 1 
	},
}

GameFunc.GoodChoices = GoodChoices
GameFunc.BadChoices = BadChoices

--//// RARITY / WEIGHT CALCULATIONS \\\\--
function GameFunc:SelectChoice(choices)
	local TotalWeight = 0
	for _, rarity in pairs(choices) do
		TotalWeight = TotalWeight + (1 / rarity.Rarity)
	end

	local RandomWeight = math.random() * TotalWeight
	local CumulativeWeight = 0

	for INDEX, rarity in pairs(choices) do
		CumulativeWeight = CumulativeWeight + (1 / rarity.Rarity)
		if RandomWeight <= CumulativeWeight then
			return INDEX, choices[INDEX]
		end
	end
end

return GameFunc
2 Likes

Thank you!! (MAX CHAR LIMIT!!)

1 Like

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