RNG System Help

how do i do that converting correctly?
btw you might be going the wrong way. My Rarities module is this

return {
	["RarityI"] = {1, 2}, -- "{1, 2}" is one in two
	["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}, -- Adding more later
}

What exactly are you trying to achieve/ why do you need “index”?

I watched the tutorial way above and they provided the index.
I’m trying to make a RNG giving in-game currency, not normal RNGs that roll for different aura.

In the tutorial, they’re using a table in which index is assigned.

In your case, you’re defining index and assigning it a string value (on which you cannot use tonumber).
You’re confusing the two indexes, in the video, the first script uses index as the current index in the table while in the second one, it’s being used to tell what rarity has been picked.

You cannot use tonumber() on strings like he stated above. Things like combinations of numbers can be converted and strings cannot. They will return nil

Im also trying to make it so that the player gets in-game currencies by their roll luck. If the player’s base is 5 and rolls an 1 in 3, 5x3 is 5x2 is 10(*3 because the plr rolled one in three). But I still can’t figure out the solution.

The second number is the number I want to add the in-game currency by.

I rewatched the video and got a different error.
Error is ServerScriptService.RollHandler:12: attempt to index nil with 'Value'

local r = game.ReplicatedStorage.Events
local Rarities = require(game.ServerStorage.Rarities)
local RarityService = require(game.ServerStorage.RarityService)

r.Roll.OnServerEvent:Connect(function(plr)
	plr.leaderstats["🎲 Rolls"].Value += 0
	local luck = 1
	for i = 1, 5 do
		local index = RarityService.chooseIndex(Rarities, luck)
		if i == 5 then
			print("Chosen rarity: "..index)
			print(index[2].Value) -- Error Line
		else
			print("Possible rarity: "..index)
		end
		wait(plr.Values.RollSpeed.Value)
	end
end)

I might be getting a lot closer to solving,

1 Like

May I see what the line before that prints?
Its apparently returning nil according to the error…?

the line before that is ok. it does not have error but this has. the modules can be above these posts if you need

1 Like

Does the index table get manipulated/deleted after that print??
do: print(index[2])
probably prints nil…
Where did you get value from? Can you slide the RarityService so I can examine?

RarityService 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
1 Like

It prints “nil”. I’m trying to get the second number of index that looks like “1, 2” which is 2
BUT when I do “print(index)”, it does not give nil. I wonder why

1 Like

This returns a string/index not a table so you can’t index it later on. If you wanted to index it do this:

To

return rarityTable[selectedRarity]

Edit: Also don’t do .Value just do Table[index] when you print

I did this “return selectedRarity, rarityTable[selectedRarity]”, didn’t do Value and did table[index] but still print out nil at the end. But when I just do “print(index)”, it prints correctly.

Okay, so I see what the problem is, the problem is that you are trying to update StarterGui but you can’t really do that because it is just a template that gives each player their Gui when they join the game. You can fix that by accessing the PlayerGui, which updates what they actually can see.

(Updated ServerScriptService Script)

local r = game.ReplicatedStorage.Events

r.Roll.OnServerEvent:Connect(function(plr)
    
    plr.leaderstats[":game_die: Rolls"].Value += 1

    
    local luck = math.random(1, 1000)

    
    local playerGui = plr:FindFirstChild("PlayerGui")
    if playerGui then
        local rollingFrame = playerGui:WaitForChild("Game"):FindFirstChild("RollingFrame")
        if rollingFrame then
            rollingFrame.Visible = true
            rollingFrame.Chance.Text = "1 in " .. tostring(luck)
            local baseValue = plr.Values.Base.Value
            rollingFrame.Amount.Text = tostring(baseValue * luck)
        end
    end
end)

(Updated leaderstats script)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local values = Instance.new("Folder")
	values.Name = "Values"
	values.Parent = plr
	
	local rolls = Instance.new("NumberValue")
	rolls.Name = "🎲 Rolls"
	rolls.Value = 0
	rolls.Parent = leaderstats
	
	local RobuxDisplay = Instance.new("StringValue")
	RobuxDisplay.Name = " Robux"
	RobuxDisplay.Value = "0"
	RobuxDisplay.Parent = leaderstats
	
	local Robux = Instance.new("NumberValue")
	Robux.Name = "Robux"
	Robux.Value = 0
	Robux.Parent = values
	
	local multiplier = Instance.new("NumberValue")
	multiplier.Name = "Multiplier"
	multiplier.Value = 1
	multiplier.Parent = values
	
	local base = Instance.new("NumberValue")
	base.Name = "Base"
	base.Value = 1
	base.Parent = values
end)

I hope this helps you!

I just had a topic about PlayerGui and has been solved. Now the hard part is adding currency using index[???] which returns nil. The problem is above these and you’ll see them.

Okay, so this might work

local rarityName, rarityData = RarityService:GetRarity()
print(rarityName, rarityData)

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!

I took your rolling code and it worked fine with me when I applied the changes. I made the RarityService return rarityTable[selectedRarity] then when I got my value I just indexed the 2nd value and I got the rarity. I don’t know why you keep doing index[2].Value when there’s no key called Value on the table you pass in.

1 Like