Is there a way to pick random values from a table that aren't numbers?

I am trying to create a spinning effect on a name roll for a game im working on. I have a table with all the names but I dont know how to make it pick random ones before settling on whatever it lands on.

I’d like to avoid just running a set of names because you would then, see it every time.

local CNRs = {
	--Ult

	guy1= .05,
	guy2= .05,
	guy3  = .05,
	guy4= .05,
	guy5= .05,

I already have it to where it selects a name from a string, but I want it to randomly scroll through a set amount of names in my table before finishing, is that possible?

Thanks.

Not sure if this is what you mean, but I thought of a way

-- This section counts how many
-- elements are in the dictionary
local length = 0
for _, _ in (table) do
	length += 1
end

-- This section picks a random
-- element from the dictionary
local counter = 0
local chosenIndex = math.random(1, length)
for key, value in (table) do
	counter += 1
	
	if counter == chosenIndex then
		-- THIS WAS CHOSEN WOOHOO!!!
		
		-- break out so it doesn't go past the chosen value
		break
	end
end

Would this work if I already have code that picks the winner? If I dont change anything, and hit spin, it will give me a random name like I want. Just the idea of having it spin is what im looking for.

Can I see what your code that picks the winner looks like?
I really don’t understand what you’re asking

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	
	local CNRs = {
		--Ult
		
		guy1= .05,
		guy2 = .05,
		guy3  = .05,
		guy4= .05,
		guy5= .05,
	}
	
	local sum = 0
	for _,Values in pairs(CNRs) do
		sum = sum + Values
	end
	
	local function getRandomItem()
		local ClanValue = Random.new():NextNumber(0, sum)
		for item, value in pairs(CNRs) do
			ClanValue = ClanValue - value
			if ClanValue < 0 then
				return item
			end
		end
	end


local DataFolder = Instance.new('Folder',Player)
DataFolder.Name = "DataFolder"

local Clan = Instance.new("StringValue", DataFolder)
Clan.Name = "Clan"

local Clan1 = Instance.new("NumberValue", DataFolder)
Clan1.Name = "Spins"
Clan.Value = getRandomItem()
Clan1.Value = 5000

end)
local RS = game:GetService("RunService")


local debounce = false

RS.Heartbeat:Connect(function()
	if debounce == true then return end
	debounce = true
	script.Parent.Text = game.Players.LocalPlayer:WaitForChild("DataFolder").Clan.Value
	task.wait(1)
	debounce = false
end)

I don’t see an issue with this script? It looks like it gets the random item just fine

Okay let me show you an example,

https://gyazo.com/ddaf5cc53e9daf53da21734f6e6c50a8

Here its spinning through random names, but its the same 5 names before it lands on the value its supposed to land on. I want it to display all different names at all times, if that makes sense.

You can index like this or just access it without indexing:

table = {
  guy1 = 0.5,
  ["guy2"] = 0.5
}

table.guy1
table["guy2"]

Doesnt that defeat the idea of it being random? If I am putting in select ones to loop through?

Maybe you could do something like:

table["guy" .. tostring(random)]

I used the guy1 naming stuff as an example, what about for just regular names?

Maybe you mean something like this:

local randomKey = keys[math.random(1, #keys)]
local randomVariable = myTable[randomKey]

Im not that great at scripting, what is the “key” referencing?

Here is what I have if this helps any more,

spinning script

As you can see from that, I have like 5 names that it circulates through, but its those same names every time. So what I am trying to do is make it go through any of the names in that clip to make it seem more genuine, if that makes sense.

Well you don’t have to do it like that but you can do it like this:

local randomNumber = math.random(1,#table)
local chosenValue = table[randomValue]

Basically, you have to convert the table into an array. Like this:

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")

	local CNRs = {
		--Ult

		guy1 = .05,
		guy2 = .05,
		guy3 = .05,
		guy4 = .05,
		guy5 = .05,
	}
	
	local keys = {}
	for k,v in pairs(CNRs) do
		table.insert(keys, k)
	end
	
	local DataFolder = Instance.new('Folder',Player)
	DataFolder.Name = "DataFolder"

	local Clan = Instance.new("StringValue", DataFolder)
	Clan.Name = "Clan"

	local Clan1 = Instance.new("NumberValue", DataFolder)
	Clan1.Name = "Spins"
	Clan.Value = math.random(1, #keys)
	print(Clan.Value)
	Clan1.Value = 5000
end)

If you need more information, you can check the following post, the OP explains in detail how it works.

I tested it so it should work.

I couldn’t find out a way to incorporate this in my own…

https://gyazo.com/6986e26cb615baf11e0a19eeeb68b7f7

While there is no error, it is just giving me numbers. Was there something I was supposed to change?

If you want to get the value instead of the index number, you can do the following:

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")

	local CNRs = {
		--Ult

		guy1 = .05,
		guy2 = .05,
		guy3 = .05,
		guy4 = .05,
		guy5 = .05,
	}
	
	local keys = {}
	for k,v in pairs(CNRs) do
		table.insert(keys, k)
	end
	
	local DataFolder = Instance.new('Folder',Player)
	DataFolder.Name = "DataFolder"

	local Clan = Instance.new("StringValue", DataFolder)
	Clan.Name = "Clan"

	local Clan1 = Instance.new("NumberValue", DataFolder)
	Clan1.Name = "Spins"
	Clan.Value = CNRs[math.random(1, #keys)]
	Clan1.Value = 5000
end)

Isn’t this the same script as the one above?

No, this line is different, now it assigns the value instead of the index.

Yeah this isn’t working for me, and I dont understand.

I’ve scrapped the changes and just put it back to what I had, Here are the three scripts I am using.

https://gyazo.com/d8a3f339cdc563fb6e89e29a19f3d20f

https://gyazo.com/ea6dd8ef0a7fed18c8c025bc776a1223

https://gyazo.com/a8f957d6339b2ca48d7648f4679edb24

This last one is the one I don’t want to use, but am using to make the spin effect.