Invalid argument #2 to 'random' (interval is empty)

So, I have a script that detects players nearby who have less than 100 hp.
I know what the problem is, it’s just that if there are no players nearby, then the table doesn’t get players in any way and I’ve already tried something and it didn’t help. I need an idea how to run the script again if the players were not detected and not get an error that will completely break the script.

				local tablet = {}
				for i,v in pairs(workspace:GetDescendants()) do
					if v.Parent:FindFirstChild("Humanoid") and v.Parent:IsA("Model") and v.Parent:FindFirstChild("Humanoid").Health ~= 100 then
						if table.find(tablet, v.Parent) then continue end
						if (v.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= 30 then
							table.insert(tablet, v.Parent)
						end
					end
				end
				local random = math.random(1,#tablet)
				local randomplayer = tablet[random]

with a patch would be enough. Just do something when the tablet is not empty.

local tablet = {}
for i,v in pairs(workspace:GetDescendants()) do
    if v.Parent:FindFirstChild("Humanoid") and v.Parent:IsA("Model") and v.Parent:FindFirstChild("Humanoid").Health ~= 100 then
        if table.find(tablet, v.Parent) then continue end 
        if (v.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= 30 then
            table.insert(tablet, v.Parent)
        end
    end
end
if #tablet > 0 then
    local random = math.random(1,#tablet)
    local randomplayer = tablet[random]
end

It really helped! I just wrote if tablet ~= nil, apparently it’s not worth doing as I did, so nothing worked. Thank you very much!

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