I am working on a card that requires me to select a random card through a folder of all possible cards. I thought a good way to do this way getting a random card through its index, but when I try to reference the card object through its index I get an error. What I want to figure out is how to reference an object through its index.
Here is what the output is.
4 is not a valid member of Folder "ReplicatedStorage.cardHolder"
I read some dev forum post, dev hub, and youtube to try to figure this out. The post used a very similar method to what I did but got it to work, while I can’t get mine to work. Has roblox changed the way of how indexing works?
local cards = game.ReplicatedStorage.cardHolder
while true do
local cardIndex = math.random(1, #cards:GetChildren())
print(cardIndex)
local card = cards[cardIndex]:Clone()
card.Parent = script.Parent.card
wait(5)
card:Destroy()
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Cards = ReplicatedStorage:WaitForChild("cardHolder")
while true do
local RandomCard = Cards:GetChildren()[Random.new():NextInteger(1, #Cards:GetChildren())]
RandomCard.Parent = script.Parent.card
task.wait(5)
RandomCard:Destroy()
RandomCard = nil
end
I should also add that you are trying to search the folder for the number 1, for example, even though the name of the card is “one”.
You might want to change your script a bit.