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.
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?
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.
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)
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.
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.