You can write your topic however you want, but you need to answer these questions:
So I have a dictionary and it has a random rng value which is assigned to npc in my game and next to the rng number is a score the bot got, now I want to find which bot gets the best score
The Issue is that I cant get the value of the bots score in the dictionary and compare it
game.ReplicatedStorage.BestFit.Event:Connect(function()
local Highest = 0
for Item, Cost in pairs(FitnessScores) do
if Highest < Cost then
Highest = Cost
end
end
end)
The error I get is -
attempt to compare number < table
I Have Tried putting a # before the Cost, but it don’t work
local Num = 1
local FitnessScores = {
}
game.ReplicatedStorage.SendFitness.Event:Connect(function(Fit, Bot)
--print(Bot)
FitnessScores["Name: " .. Bot .. " Num: " .. Num] = {Fit}
--print(FitnessScores)
Num += 1
end)
game.ReplicatedStorage.BestFit.Event:Connect(function()
local Highest = 0
for Item, Cost in pairs(FitnessScores) do
if Highest < Cost[Num] then
Highest = Cost
end
end
end)
Looks like from just a quick reading it’s maybe returning a table and not a number that it’s able to check. You will need to specify when checking the highest number that it checks the returned tables number and not just the table.
Omg ty, For people with same problem here is the script
game.ReplicatedStorage.BestFit.Event:Connect(function()
local Highest = 0
for Item, Cost in pairs(FitnessScores) do
if Highest < tonumber(Cost[1]) then
Highest = tonumber(Cost[1])
print(Highest)
end
end
end)
since the Cost was printing [1] = “34.65345” I changed “Cost” to ToNumber(Cost[1])
Make sure also, that tonumber() ~= nil, to avoid any errors:
game.ReplicatedStorage.BestFit.Event:Connect(function()
local Highest = 0
for Item, Cost in pairs(FitnessScores) do
if tonumber(Cost[1]) and Highest < tonumber(Cost[1]) then
Highest = tonumber(Cost[1])
print(Highest)
end
end
end)