So I watched the tutorial in the very top, added a twist, and got this error: attempt to index nil with 'Value'
. I rewatched and followed the tutorial carefully but still got the error.
Rarities Script(Module):
return {
["RarityI"] = {1, 2}, -- 1 in 2
["RarityII"] = {1, 3},
["RarityIII"] = {1, 4},
["RarityIV"] = {1, 5},
["RarityV"] = {1, 7},
["RarityVI"] = {1, 10},
["RarityVII"] = {1, 15},
["RarityVIII"] = {1, 20},
["RarityIX"] = {1, 25},
["RarityX"] = {1, 35},
["RarityXI"] = {1, 50},
["RarityXII"] = {1, 75},
["RarityXIII"] = {1, 99},
["RarityXIV"] = {1, 100},
["RarityXV"] = {1, 125},
["RarityXVI"] = {1, 200},
["RarityXVII"] = {1, 250},
["RarityXVIII"] = {1, 333},
["RarityXIX"] = {1, 450},
["RarityXX"] = {1, 500},
["RarityXXI"] = {1, 666},
["RarityXXII"] = {1, 777},
["RarityXXIII"] = {1, 900},
["RarityXXIV"] = {1, 999},
["RarityXXV"] = {1, 1000},
["RarityXXVI"] = {1, 1111},
["RarityXXVII"] = {1, 1250},
["RarityXXVIII"] = {1, 1333},
["RarityXXIX"] = {1, 1400},
["RarityXXX"] = {1, 1500},
["RarityXXXI"] = {1, 1750},
["RarityXXXII"] = {1, 2000},
["RarityXXXIII"] = {1, 2025},
["RarityXXXIV"] = {1, 2222},
["RarityXXXV"] = {1, 2500},
["RarityXXXVI"] = {1, 2750},
["RarityXXXVII"] = {1, 3000},
["RarityXXXVIII"] = {1, 3333},
["RarityXXXIX"] = {1, 4000},
["RarityXL"] = {1, 5000}, -- 1 in 5,000
}
RarityService Script(Module):
local Module = {}
function Module.chooseIndex(rarityTable, luck)
local newRarityArray = {}
local totalWeight = 0
for index, rarity in pairs(rarityTable) do
local weight = rarity[2]
local newWeight = weight - luck
if newWeight < 1 then
continue
end
local fraction = (1 / newWeight)
totalWeight += fraction
newRarityArray[index] = {fraction}
end
local random = Random.new()
local rnd = random:NextNumber(0, totalWeight)
local selectedRarity = "RarityI"
local accumulatedWeight = 0
for index, rarity in pairs(newRarityArray) do
accumulatedWeight += rarity[1]
if rnd <= accumulatedWeight then
selectedRarity = index
break
end
end
return selectedRarity
end
return Module
Picking Rarity Script:
local r = game.ReplicatedStorage.Events
local Rarities = require(game.ServerStorage.Rarities)
local RarityService = require(game.ServerStorage.RarityService)
-- local NumberFormatter = require(game:GetService("ServerScriptService"):WaitForChild("CommaFormatter"))
r.Roll.OnServerEvent:Connect(function(plr)
plr.leaderstats["🎲 Rolls"].Value += 0
local luck = plr.Values.Luck.Value
for i = 1, 5 do
local index = RarityService.chooseIndex(Rarities, luck)
if i == 5 then
print("Chosen rarity: "..index)
plr.Values.Robux.Value += index[2].Value * plr.Values.Base.Value
--plr.PlayerGui.Game.RollingFrame.Visible = true
-- local number = tonumber(index[2])
--local formattedNumber = NumberFormatter.biggerFormat(number)
--plr.PlayerGui.Game.RollingFrame.Chance.Text = "1 in "..formattedNumber
--plr.PlayerGui.Game.RollingFrame.Amount.Text = plr.Values.Base * index[2]
else
print("Possible rarity: "..index)
end
wait(plr.Values.RollSpeed.Value)
end
end)
The reason I’m trying to do index[2]
is because it is the selected rarity and the second number(1 in 2) (but 2 is the one I’m trying to pick) but it prints nil and that’s the cause of the error. When I print “index”, it does NOT show nil and works perfectly. I hope you understand this post and be able to help!