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",
}))
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.
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)
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.