I’ve encounter some problems regarding with string.split() . Im trying to make reward system for team that has the highest score . Thats all.
table = {
[1] = "Red 0",
[2] = "Blue 0"
}
I have this table or dictionary or whatever its called and what I want to do is get the number from value using string.split.
here is my approach . kinda messy though
function Round:FindWinner()
local count = {}
local number = 0
for i , team in pairs(Teams:GetTeams()) do
count[i] = team.Name .. " " .. team:GetAttribute("IntelCollected")
end
--[[
Output:
table {
[1] = "Red 0",
[2] = "Blue 0"
}
]]--
for key , value in pairs(count) do
print(count)
if string.split(count[key] , " ")[2] == string.split(count[key + 1 ], " ")[2] then -- check if draw
warn("Draw")
break
else -- if not draw
if string.split(value , " ")[2] > tostring(number) then -- check if the number inside value is higher than number
number = tonumber(string.split(value , " ")[2])
warn(string.split(value , " ")[1] .. "Winner") -- getting the winner name
end
end
end
end
this line
if string.split(count[key] , " ")[2] == string.split(count[key + 1 ], " ")[2] then
throw an error when one of the team has the highest score.
the error
invalid argument #1 to 'split' (string expected, got nil)
This may help … You’ll have to rewrite it to suit your needs. It just figures the winner of the array.
local tbl = {
[1] = "Red 0",
[2] = "Blue 0"
}
local highestValue = -math.huge
local winningTeams = {}
for index, value in pairs(tbl) do
local number = tonumber(string.match(value, "%d+"))
if number and number > highestValue then
highestValue = number
winningTeams = {index}
elseif number and number == highestValue then
table.insert(winningTeams, index)
end
end
if #winningTeams > 1 then
print("It's a tie between the following teams:")
for _, teamIndex in ipairs(winningTeams) do
print(tbl[teamIndex])
end
else
print("The winning team is: " .. tbl[winningTeams[1]])
end
Also try to avoid using the word “table” for a table.
table is the name of a global table (yeah) that has functions like table.sort and table.insert in it. Making your own variable named table will either overrwrite (if not local) or shadow (if local) this, which is usually not what you meant to do.