Problem with table randomizer

Lost Brain Cells.txt (1.9 KB)
What happens? The table selects the warning instead of the table items, and then you lose brain cells! Please help the table select the items only and not the warning part. Thank you :slight_smile:

1 Like

The Shiny_Rarities table is not returning the name of it because it’s name is not a string.

Before:

local Shiny_Rarities = { -- ObjectName = frequency (frequency must be an integer)
	Shiny = 4,
	NonShiny = 96
}

After:

local Shiny_Rarities = { -- ObjectName = frequency (frequency must be an integer)
	["Shiny"] = 4,
	["NonShiny"] = 96
}

Having it like that does not matter for a table

1 Like

It’s because you’re calling the Roll2 function twice so it can give a different answer each time. You should store it in a variable, then check the names

2 Likes

Something like this would work:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.Purchase

PurchaseEvent.OnServerEvent:Connect(function(player)
	if player then
		task.wait(0.03)
		
		local Roll2 = Roll2()
		
		if Roll2 == "Shiny" then
			task.wait(0.1)
			print("Success - Shiny")
			-- Other stuff
		elseif Roll2 == "NonShiny" then
			task.wait(0.1)
			print("Success - NonShiny")
			-- Other stuff
		else 
			warn("Roll Failed")
		end
	end
end)
1 Like

I’m getting an error “attempt to call a string value” on the line that stores Roll2 in a variable.

I would like to see your idea of the code for this. Fittergem1000’s script did not work. Storing the Roll2 function in variable causes it to return an error saying you’re attempting to call a string. The 2nd table is the one that keeps erroring. Roll1 always works for some reason the 2nd table is the one causing the errors.

I am not sure why the Roll2 variable is erroring. Logically it doesn’t make sense.

local function Roll2() -- ObjectName [string]
	return RollTable2[math.random(1, #RollTable2)]
end

The function Roll2() returns a string from the RollTable2 table.

The table is created when we do:

for object_name, frequency in pairs(Shiny_Rarities) do
	for i = 1, frequency do
		table.insert(RollTable2, object_name)
	end
end

Unless the table is empty, or it doesn’t contain any strings, there is no reason that the Roll2 variable should be erroring.

1 Like

I don’t wish to give my exact game away because it’s my own work and it’s not fair for me to have to do that. I am busy tonight, I greatly appreciate your fast response time. I will create a completely separate basic example game for this exact problem as soon as I get the chance, and you can test it and see the problem exactly as it is. Thank you so much for taking the time to help me :grinning:. I will respond within the next few hours

Yeah theirs should’ve worked, anyways I’d recommend not even using if statements and just storing data so you can index it without manually defining a path for each one

1 Like

Lost Brain Cells.rbxl (58.2 KB)
I was able to create it very quickly before I gtg. Here is the example game with the error in it. You can use this to find the exact solution to this problem

I appreciate the effort to help me understand how to help you understand your problem (lol).

I do have the code you’ve given me in a separate game and after some edits it is working just fine.

Here is the code that I have working: (I noted out all of the extra functions of each if statement. I would recommend creating a separate function for dealing with editing the player GUI and cloning items, as it makes everything more organized and makes it easier to pinpoint errors in your code…just something I would do but isn’t required)

-- Braincell Script (ServerScriptService)

-- Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.Purchase

-- Tables
local Angels = { -- ObjectName = frequency (frequency must be an integer)
	["Rainbow_Angel"] = 10,
	["Death Angel"] = 40,
	["Holy Angel"] = 50,
}

local Shiny_Rarities = { -- ObjectName = frequency (frequency must be an integer)
	Shiny = 4,
	NonShiny = 96
}

local RollTable1 = {}
local RollTable2 = {}

-- Functions
local function Roll1() -- ObjectName [string]
	return RollTable1[math.random(1, #RollTable1)]
end

local function Roll2() -- ObjectName [string]
	return RollTable2[math.random(1, #RollTable2)]
end

-- Init
for object_name, frequency in pairs(Angels) do
	for i = 1, frequency do
		table.insert(RollTable1, object_name)
	end
end

for object_name, frequency in pairs(Shiny_Rarities) do
	for i = 1, frequency do
		table.insert(RollTable2, object_name)
	end
end

PurchaseEvent.OnServerEvent:Connect(function(player)
	if player then
		task.wait(0.03)
		
		local Roll2 = Roll2()
		
		if Roll2 == "Shiny" then
			task.wait(0.1)
			print("Success - Shiny")
			-- Do other stuff
		elseif Roll2 == "NonShiny" then
			task.wait(0.1)
			print("Success - NonShiny")
			-- Do other stuff
		else 
			warn("Roll Failed")
		end
	end
end)

I also reordered a few things to make it more organized for me as I was testing, but you can put everything back to how it was. I just standardized it.


Going back to creating functions to manage your “other stuff” in your if statements, if you replace this:

PurchaseEvent.OnServerEvent:Connect(function(player)
	if player then
		task.wait(0.03)
		
		local Roll1 = Roll1()
		local Roll2 = Roll2()
		
		if Roll2 == "Shiny" then
			task.wait(0.1)
			print("Success - Shiny")
			player.PlayerGui["Angel Gui"]["Angel Label"].Text = "Shiny " .. Roll1()
            local Name_of_Item = player.PlayerGui["Angel Gui"]["Angel Label"].Text
            local The_Item_Received = game.ReplicatedStorage.Towers[Name_of_Item]
            local clone = The_Item_Received:Clone()
            if player.Backpack:FindFirstChild(Name_of_Item) then
	            clone.Parent = nil
            else 
	            clone.Parent = player.Backpack 
            end
		elseif Roll2 == "NonShiny" then
			task.wait(0.1)
			print("Success - NonShiny")
			player.PlayerGui["Angel Gui"]["Angel Label"].Text = Roll1()
            local Name_of_Item = player.PlayerGui["Angel Gui"]["Angel Label"].Text
            local The_Item_Received = game.ReplicatedStorage.Towers[Name_of_Item]
            local clone = The_Item_Received:Clone()
            if player.Backpack:FindFirstChild(Name_of_Item) then
	            clone.Parent = nil
            else 
	            clone.Parent = player.Backpack 
            end
		else 
			warn("Roll Failed")
		end
	end
end)

With this:

local function Edit_Gui(player: Player, text: string)
	local player_gui = player.PlayerGui
	local angel_label: TextLabel = player_gui["Angel Gui"]["Angle Label"]
	angel_label.Text = text
	return text
end

local function Clone_Item(player: Player, Name_of_Item: string)
	local The_Item_Received = game.ReplicatedStorage.Towers[Name_of_Item]
	local clone = The_Item_Received:Clone()
	
	if player.Backpack:FindFirstChild(Name_of_Item) then
		clone.Parent = nil
	else 
		clone.Parent = player.Backpack 
	end
end

PurchaseEvent.OnServerEvent:Connect(function(player)
	if player then
		task.wait(0.03)
		
		local Roll1 = Roll1()
		local Roll2 = Roll2()
		
		if Roll2 == "Shiny" then
			task.wait(0.1)
			print("Success - Shiny")
			local Name_of_Item = Edit_Gui(player, "Shiny "..Roll1)
			Clone_Item(Name_of_Item)
		elseif Roll2 == "NonShiny" then
			task.wait(0.1)
			print("Success - NonShiny")
			local Name_of_Item = Edit_Gui(player, Roll1)
			Clone_Item(Name_of_Item)
		else 
			warn("Roll Failed")
		end
	end
end)

If you want to test this out you can. I will open up the place you sent and test that out.

After opening the place and messing around, I found the issue.

Inside of your if statement, you are comparing the function, not the variable to the string:

image

What you need to do instead is this:

PurchaseEvent.OnServerEvent:Connect(function(player)
	if player then
		task.wait(0.03)
		local Roll2 = Roll2()
		
		if Roll2 == "Shiny" then
			task.wait(0.1)
			print("Success")
			print(Roll1())
		elseif Roll2 == "Non Shiny" then
			task.wait(0.1)
			print("Success")
			print(Roll1())
		else 
			warn("Roll Failed")
		end
	end
end)

The other thing I noticed was that when you compared:

elseif Roll2 == "Non Shiny"

Originally it was “NonShiny”, however, in your table it was named “Non Shiny”.

Here is the place with the updated script:

LostBrainCells (Updated).rbxl (58.4 KB)

1 Like

This was the solution. Thank you so much :smile:

1 Like

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