The silliest error I have ever experienced

ok so this is VERY simple. I have a table that i need to go in a certain order (it’s a chance table and i want to do the largest ones first) and for WHO KNOW’S what reason, it is processing in the… alphabetic order of the numbers? it’s meant to go 100, 22, 10, 5 but because of how numbers and stuff works, it’s going 10, 100, 22, 5, which means the 10 is completely ignored???

(Also, it’s a pairs table. whenever i tried using table.sort i just got an error.)

Could we see a little more of the code, especially where you’re having problems with?

alright.

It processes sword chances in the order: 10,100,22,5

local SwordChances = {
	["Sword"] = 100;
	["Dull Sword"] = 22;
	["Sharp Sword"] = 10;
	["Longsword"] = 5;
}

local Swords = {
	"Sword";
	"Dull Sword";
	"Sharp Sword";
	"Longsword";
}

local function run(hit)
	local runfunction = false
	for i,v in game.Players:GetChildren() do
		if hit.Parent.Name == v.Name then runfunction = true
	end
		
	if runfunction == true then
		local chance = math.round(math.random(1,10))
		local currentTool
			
		for i,v in pairs(SwordChances) do
				print("i: "..i)
			if chance<v then currentTool = i end
		end
			
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		local giveTool = true
			
		for i,v in plr.Backpack:GetChildren() do
			if table.find(Swords,v.Name) then giveTool = false end
		end
			
		for i,v in hit.Parent:GetChildren() do
			if table.find(Swords,v.Name) then giveTool = false end
		end
			
		if giveTool == true then
			local tool = game.ReplicatedStorage:WaitForChild(currentTool):Clone()
			tool.Parent = plr.Backpack
		end
		end
	end
	wait(1)
end

script.Parent.Touched:Connect(run)

I need it to process in 100,22,10,5 order because with the code I use, the larger number always beats smaller ones if it is later in the list. Right now, the 10 (Sharp sword) never is pulled.

Can you write the table like this instead? Because this works for me:

local SwordChances = {{"Sword", 100}, {"Dull Sword", 22}, {"Sharp Sword", 10}, {"Longsword", 5}}

for i, v in pairs(SwordChances) do

print(i .. ": Name: " .. v[1] .. ". Value: " .. v[2] ..".")

end

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