String <= number error [both are numbers]

Code:

pet.OnServerEvent:Connect(function(player, egg)
	if game.Workspace.ActualEggs:FindFirstChild(egg) then
		local Egg = game.Workspace.ActualEggs:FindFirstChild(egg)
		--[[if (Egg.Egg.Position - player.Character.HumanoidRootPart.Position).magnitude >= 15 then
			warn("Player is not close enough!")
			return;
		end]]
		local petsThatCanBeWon = require(Modules.EggData)[egg];
		local assortedData = {};
		local Chances = petsThatCanBeWon.Pets;
		local Pull = 0;
		local winner = Random.new():NextNumber(1, 100)
		local winner2 = nil;
		print(winner .. " - " .. typeof(winner))
		print(Pull .. " - " .. typeof(Pull))
		for petname, chance in pairs(Chances) do
			Pull += chance
			if winner <= Pull then
				winner = petname;
			end
		end
		pet:FireClient(player, egg.Name, winner2);
	end
end)

Both pull, and winner is a number. When I do the print’s, it says:
98.69132335165024 - number - Server - Game Controller:176
22:43:04.460 0 - number

Neither one is a string.

2 Likes

Just curious, because lua can be a little wonky, try changing

-- original
print(winner .. " - " .. typeof(winner))
-- to
print(winner, tonumber(winner), " - ", typeof(winner))

-- and
-- original
print(Pull .. " - " .. typeof(Pull))
-- to
print(Pull, tonumber(Pull), " - ", typeof(Pull))

New prints:

27.18697712367071 27.18697712367071  -  number  -  Server - Game Controller:176
  22:50:55.675  0 0  -  number 

Same thing as before, except the number is posted twice.

1 Like

Wait, certain it is on that line, because that error really shouldn’t be happening.

It is. The only comparsion in that function atleast, and I matched the lines multiple times.

Also, I removed the mag check to see if it was there and the problem still persist. My eggData module holding the chance value could be the problem. Here that is:

local assortedEggData = {}
local eggs = {
	["Starter Egg 1"] = {
		Price = 750,
		Pets = {
			["Doggy"] = 50,
			["Kitty"] = 30,
			["Bunny"] = 10,
			["Bear"] = 8,
			["Panda"] = 1.9999999886869,
			["[SECRET] Classic Trio"] = 0.0000000113131,
		}
	},
	["Starter Egg 2"] = {
		Price = 750,
		Pets = {
			["Fox"] = 40,
			["Polar Bear"] = 30,
			["Wolf"] = 15,
			["Dino"] = 10,
			["Dragon"] = 4.9999999999999005,
			["[SECRET] Hat O' Pets"] = 0.0000000000001,
		}
	},
}
assortedEggData.__index = assortedEggData;
return setmetatable(eggs, assortedEggData);

I didn’t use metatables before, switched to using it and still had the problem so, losing hope.

1 Like

Try printing chance in the loop, and see what it says.

1 Like
15.955028512877643 - number  -  Server - Game Controller:176
  23:01:37.599  0 - number  -  Server - Game Controller:177
  23:01:37.599  30 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179
  23:01:37.599  10 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179

ERROR:
ServerScriptService.Scripts.Game Controller:181: attempt to compare string <= number - Server - Game Controller:181

1 Like

Now I am extremely confused, I guess print winner in the loop, since the error is calling it a string.

Bit of a longer print.

95.18894518270578 - number  -  Server - Game Controller:176
  23:07:48.469  0 - number  -  Server - Game Controller:177
  23:07:48.469  30 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179
  23:07:48.469  95.18894518270578 - number <- THIS IS THE WINNER FROM THE LOOP  -  Server - Game Controller:181
  23:07:48.469  10 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179
  23:07:48.469  95.18894518270578 - number <- THIS IS THE WINNER FROM THE LOOP  -  Server - Game Controller:181
  23:07:48.469  1.13131e-08 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179
  23:07:48.469  95.18894518270578 - number <- THIS IS THE WINNER FROM THE LOOP  -  Server - Game Controller:181
  23:07:48.469  1.9999999886869 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179
  23:07:48.469  95.18894518270578 - number <- THIS IS THE WINNER FROM THE LOOP  -  Server - Game Controller:181
  23:07:48.469  50 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179
  23:07:48.469  95.18894518270578 - number <- THIS IS THE WINNER FROM THE LOOP  -  Server - Game Controller:181
  23:07:48.470  8 - number <- THIS IS THE CHANCE PRINT!  -  Server - Game Controller:179
  23:07:48.470  95.18894518270578 - number <- THIS IS THE WINNER FROM THE LOOP  -  Server - Game Controller:181
1 Like

You know what, I just found the issue. I was setting winner to the petname, after it chose a winner, without actually breaking the loop so it would continuing looping and erroring after that.

I’m so sorry for any headache cost. It took a moment to realize that this is the only time it ran through, since it didn’t have to run another loop after it found the winner [since it picked the last index].

1 Like

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