Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Choose A Random Pet From A table?

Asked by 6 years ago
local RP = game.ReplicatedStorage
local Directory = {
Pets = {
["Dog"] = {
    Name = "Dog",
    Rarity = "Common",
    Level = "1"
},
["Cat"] = {
    Name = "Cat",
    Rarity = "Common",
    Level = "1"
    }
}
}
RP["OpenEgg"].OnServerEvent:Connect(function(plr, arg1, arg2)
    local Pet = Directory.Pets[math.random(1, #Directory.Pets)]
    print(Pet)
end)

its telling me #Directory.Pets is 0 when it clearly isnt

0
These answers are horrible, feel bad for you man, try #Directory.Pets:GetChildren() greatneil80 2647 — 6y

2 answers

Log in to vote
1
Answered by
rokedev 71
6 years ago

rather then using math.random i prefer to use the Random.new()

for example

local rng = Random.new()

print(rng:NextInteger(5, 10))
-- will print a value between 5 and 10
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

you should use math.random(number) to get a random number to use as a randomizer to pick a pet from the table

Answer this question