What is wrong with with my code?

I am trying to code an RNG system, but on line 14, it always errors “attempt to index nil with code”
How can I fix this?

local D = false
local function GetRarity(Rarity1, Rarity2)
	local Random = math.random(1, Rarity1)
	if Random > Rarity2 then
		return true
	else
		return false
	end
end

local function PrintRarity(Rarities: {})
	local HighestData = 0
	for i, Data in pairs(Rarities) do
		if Data[1] > HighestData[1] then
			HighestData = Data
		end
	end
	return HighestData
end
--game.ReplicatedStorage.RemoteEvent:FireClient(Plr, C)print(GetRarity(1, 0))
	
	print(PrintRarity({
		[1] = "Easy",
		[5] = "Hard",
		[15] = "Insane",
	}))

which line is line 14 :sob:
Be more specific please

1 Like
if Data[1] > HighestData[1] then

how did you find this so quickly

HighestData is 0, not a table
Therefore, highestdata[1] is saying 0[1], which does not exist. I would also double-check that Data[1] is also a table

1 Like

well, we need to set highest data to a table afterward. how can i change this to a table that will work?

HighestData = {0}, or something like that. Just make sure the type ofe HighestData is a table. Up to you how you want to manage it

1 Like


sorry if im taking too long, im watching someone play undertale on youtube

Data[1] is nil now. Make sure it exists and that Data is a table. The rest you should be able to figure out. Learn to read your error messages instead of relying on others.

1 Like

no, its not nil, its the highestdata. i set it to {0}. read more of this.

it says
“number < nil”
the nil is the GREATER value.

if Data[1] > HighestData[1] then

which one is the greater value here?

Send me your updated code.

1 Like
local D = false
local function GetRarity(Rarity1, Rarity2)
	local Random = math.random(1, Rarity1)
	if Random > Rarity2 then
		return true
	else
		return false
	end
end

local function PrintRarity(Rarities: {})
	local HighestData = {0}
	for i, Data in pairs(Rarities) do
		if Data[1] > HighestData[1] then
			HighestData = Data
		end
	end
	return HighestData
end
--game.ReplicatedStorage.RemoteEvent:FireClient(Plr, C)print(GetRarity(1, 0))
	
	print(PrintRarity({
		[1] = "Easy",
		[5] = "Hard",
		[15] = "Insane",
	}))
	
game["Run Service"].Heartbeat:Connect(function()
	if math.abs(script.Parent.Parent.Parent.Parent.Humanoid.MoveDirection.Magnitude) > 0 and D == false then
		D = true
		wait(3)
		script.Parent:Play()
		wait(2)
		--local Stuff = game.ServerStorage.EntityMorphs:GetChildren()[math.random(1, #game.ServerStorage.EntityMorphs:GetChildren())]
		--[[if math.random(1, 300) == 1 then
			Stuff = game.ServerStorage.EntityMorphs:GetChildren()[math.random(1, #game.ServerStorage.EntityMorphsUltimate:GetChildren())]:Clone()
		else
			Stuff = game.ServerStorage.EntityMorphs:GetChildren()[math.random(1, #game.ServerStorage.EntityMorphs:GetChildren())]:Clone()
		end]]
		local T = game.TweenService:Create(script.Parent, TweenInfo.new(2, Enum.EasingStyle.Linear), {["PlaybackSpeed"] = 0.5})
		local T2 = game.TweenService:Create(script.Parent.Parent.PointLight, TweenInfo.new(1, Enum.EasingStyle.Linear), {["Color"] = Stuff.Torso.NeckAttachment.PointLight.Color})
		T:Play()
		T2:Play()
		T.Completed:Wait()
		local PLayer = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)
		Stuff.Parent = workspace
		Stuff:PivotTo(PLayer.Character.HumanoidRootPart.CFrame)
		script.Parent.Parent.Parent.Parent:Destroy()
		game.ReplicatedStorage.RemoteEvent:FireClient(PLayer, Stuff)
		PLayer.Character = Stuff
	end
end)

(full script)

Rarities = {
[1] = "Easy",
[5] = "Hard",
[15] = "Insane",
}
for i, STRING in pairs(Rarities) do
HighestData = {0}
if STRING[1] > HighestData[1] then

end
end

It’s Data. Not highest data.
You’re looping through a table that has a string assigned to a number [number]: string
and in the for loop, you’re doing i,v in Rarities

i would be [1]
v, or Data would be “Easy”
Data[1] would be greater than “Easy”[1] which is nil.

Rarities = {
[i] = v,
[i] = v,
[index] = value,
}
that’s how tables work

My previous statement was correct. Please read your code more carefully before asking basic questions.

1 Like

how do i get the value of highestdata…?

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