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;
}
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.
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)
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.
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)
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 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