Why isn't the print statement printing the chosen arrays?

I’m trying to get the system to print what the Roll() function has chosen, however, regardless of this it doesn’t seem to be printing anything, regardless of how I try to change it… Below you’ll find the code that I have, as well as the array.

Possibly problematic code

function Roll()
	local CurrentBestRarity = "Rock"
	
	for RarityTier, Chance in OreArray do
		local NewRoll = Rand:NextNumber()
		if NewRoll < Chance then
			if OreArray[CurrentBestRarity] > Chance then
				CurrentBestRarity = RarityTier
			end
		end
	end
	return CurrentBestRarity
end

local result = Roll()
local spawnBricks = game.Workspace.Spawns:GetChildren()
local Index = math.random(#spawnBricks)
local Chosen = spawnBricks[Index]

local FoundLocation = Spawns:FindFirstChild(Chosen.Name)

local function OteSpawn()
	local IsOccupied = Chosen.Occupied
	if not IsOccupied then
		if result == OreArray[1] then
			print(OreArray[1])
	
		if result == OreArray[2] then
			print(OreArray[2])
			
		end
		end
	end
end

The array

local OreArray = {
	Rock = 0.35;
	IronOre = 0.25;
	GoldOre = 0.1;
	AzureOre = 0.01;
}
2 Likes

That’s because your ore array is not actually an array, it’s a dictionary.

Arrays will have numerical values (like what you’re doing with OreArray[1]). With a dictionary you have to access the value differently:
OreArray.Rock, OreArray.IronOre, etc

Really all you have to do to access the value is
OreArray[result], since that’s similar to the above, except we’re using a variable as the index instead of the raw value.

This should work as intended right?

local function OteSpawn()
	local IsOccupied = Chosen.Occupied
	if not IsOccupied then
		if result == OreArray.Rock then
			print(OreArray.Rock)
	
		if result == OreArray.IronOre then
			print(OreArray.i)
			
		end
		end
	end
end

Players.PlayerAdded:Connect(OteSpawn)
1 Like

Adding onto what @Locard said, your if statements aren’t correctly formatted either:

This will be read as:

if result == OreArray[1] then
   if result == OreArray[2] then

   end
end
1 Like

Ahh. I see! I’ll try this out.

1 Like

Not quite, in your example you’re accessing the values within the dictionary, which would be your chance numbers and not the index. Your result value is going to be a "string". The indices within your OreArray are also "strings". So you’re just matching up whether the result is a specific string or not:

if result == "Rock" then
    -- rock stuff
elseif result == "IronOre" then
    -- paper stuff
end

Because of how your roll function works, you’re guaranteed that the result is going to be one of the string indices within the OreArray table.

1 Like

Hm… Despite changing the code to this, I’m still not seeing any printing within the command bar…

local function OteSpawn()
	local IsOccupied = Chosen.Occupied
	if not IsOccupied then
		if result == "Rock" then
		print("You got a rock!")
		elseif result == "IronOre" then
		print("You got an iron ore!")
			
		end
	end
end

Players.PlayerAdded:Connect(OteSpawn)
1 Like

Add this bit after you connect to PlayerAdded:

for _, player in game.Players:GetPlayers() do
    task.spawn(OteSpawn)
end

Then try again. This is a common issue that a lot of devs tend to overlook (more in-depth explanation here).

Strange… Even after adding this to the end, nothing prints unfortunately.

local function OreSpawn()
	local IsOccupied = Chosen.Occupied
	if not IsOccupied then
		if result == "Rock" then
		print("You got a rock!")
		elseif result == "IronOre" then
		print("You got an iron ore!")
			
		end
	end
end

Players.PlayerAdded:Connect(OreSpawn)
for _, player in game.Players:GetPlayers() do
	task.spawn(OreSpawn)
end

I made sure to check if my Roll() function works, and it did, so this is an OreSpawn() issue I believe.

What is IsOccupied? Is it a BoolValue?

Yes, it is a boolvalue assigned to each part, to make sure that there’s no model overlay.

So that’s why
You forgot to add .Value to it.

local IsOccupied = Chosen.Occupied.Value

I suggest you to use attributes but I guess BoolValue works

Oh, this works! Thanks a whole lot.

I really went back and forth for over an hour talking about the major problems that OP will run into, only for the final problem and solution to be THAT.

Oversight sucks, but we finally got to the solution :muscle:

Yup… That’s scripting for you!

1 Like

Well there are actually two solutions for that.

  1. Attempting to index number into a dictionary

  2. Overlooked variable

Yall helped this man to be a better scripter!

1 Like

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