Need help with string

Hi,

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)

I dont understand what is nil here

1 Like

The result of count[key + 1 ] is nil. Probably key + 1 is 3 in the case where it errors.

1 Like

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.

@azqjanna yep it was printing nil . since its only 2 teams so I decided that to use number instead of key

if string.split(count[key] , " ")[2] == string.split(count[key + 1 ], " ")[2] then

this line I change to this

if string.split(count[1] , " ")[2] == string.split(count[2 ], " ")[2] then

and it works perfectly…

thanks :smile:

also @2112Jay thank you for helping . I appreciated it :grin:

No problem. I ran into a slight problem with going over 9. So that version will do a number over 9 … like 100 to 1 … lol.

also @2112Jay

what do you mean by that . kinda confuse here

It seems .lua has a problem with calling a table 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.

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