Help with fixing this table related problem!

Hello! I’m currently experiencing this issue with tables that i have no idea how to fix! Any help would 100% be appreciated!

I’m trying to check if the “DroppedBy” value is whatever the current string is. (In the lower script) And if it is, add it to a table.

For some reason, it doesn’t add anything if it’s DroppedBy string equals the current string. I’m just trying to filter through every string and see if it equals that. value.

This is the module code to get all the chances:

local weapons = {}

weapons.Items = {
{ItemName="Stick",WeaponMaxChance=50,lvlRequirement=0,DroppedBy="Neutral Sans",Free=true},
{ItemName="Toy Knife",WeaponMaxChance=30,lvlRequirement=3,DroppedBy="Neutral     Sans",Free=false},
}	
return weapons

Script for actually checking the values:

local chanceTable = {}

local weaponModule = require(game.ReplicatedStorage.ModuleStorage.WeaponsModule)

local chanceTable = {}

local stuff = weaponModule.Items
for i = 1, #stuff do
local check = stuff[i].DroppedBy
if check == "Neutral Sans" then
	chanceTable[stuff[i].ItemName] = stuff[i].WeaponMaxChance
end
end

local chanceModule = require(game.ReplicatedStorage.ModuleStorage.ChanceModule)
local item = chanceModule.RandomChance(chanceTable)
print(item) 

I hope that made sense, if it didn’t, leave a comment. Thank you so much again!

Forgive me if I’m wrong (I’m not sure what you want to do here), but it seems that you are using a '~=' instead of a '==', which checks if the current string is not equal to 'Neutral Sans'. You could trying switching it to '==' to see if that works.

Edit, Forgot to revert my test sheet, fixed code

That’s exactly what i forgot haha, it sadly still doesn’t work as intended.

1 Like

First off, you should really organize your tables so they’re more readable

local weapons = {}

weapons.Items = {
	{
		ItemName="Stick",
		WeaponMaxChance=50,
		lvlRequirement=0,
		DroppedBy="Neutral Sans",
		Free=true
	},
	{
		ItemName="Toy Knife",
		WeaponMaxChance=30,
		lvlRequirement=3,
		DroppedBy="Neutral     Sans",
		Free=false
	},
}	
return weapons

Second, I slightly restructured your code, and this seems to work for me, at least the DroppedBy checking

local chanceTable = {}

local weaponModule = require(game.ReplicatedStorage.ModuleStorage.WeaponsModule)
local chanceModule = require(game.ReplicatedStorage.ModuleStorage.ChanceModule)

local stuff = weaponModule.Items
for _, tbl in ipairs(stuff) do
	local check = tbl.DroppedBy
	if check == "Neutral Sans" then
		chanceTable[tbl.ItemName] = tbl.WeaponMaxChance
		print(tbl.ItemName)
	end
end

local item = chanceModule.RandomChance(chanceTable)
print(item) 
1 Like

When putting it to ==, It doesn’t add anything to the table list. Which is very bizzare.

1 Like

Hm, very odd, says the interval is empty even after adding in your code.
EDIT: I have my tables cram packed due to the amount of items i’m going to add later on

can you post the ChanceModule code

Here you go:

local module = {}

function module.RandomChance(tabl)
local WeightedChances = {}

for Type, Value in next, tabl do 
	for Loop = 1, Value do 
		table.insert(WeightedChances, tostring(Type)) 
	end
end

return WeightedChances[math.random(1, #WeightedChances)] 
end

return module

I’m not sure what to say, it works just fine for me
image

I’ll follow up in DM as to not flood this topic with bumps

Alright, I’ll DM you in just one second. Parent’s need me to do something haha

Was that really necessary lol? I was tired and my grammar wasn’t too good then.