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

How would I use .random in GetObject?

Asked by 10 years ago

I'm trying to make a plugin where it generates an empty house randomly, there are gonna be 3 houses.

How would I make be a random house each time I click it?

assets = {"168573570","168574001"}
rhouse = game:GetObjects("rbxassetid://ID")

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

What we want is to get a random value from assets when all we can do is generate random numbers using math.random.

So, where are numbers in this problem that we could ask for with math.random? The numbers in this situation are the indices of assets, e.g., assets[1] is "168573570" and assets[2] is "168574001".

So we just generate a random index from assets and use the asset at that index.

local randomAssetIndex = math.random(1, #assets);
-- Pick a random number [1, 2, .. , length of `assets`]
local randomAsset = assets[ randomAssetIndex ];

rhou = game:GetObjects("rbxassetid://" .. randomAsset);

Note that the quotes on your assets aren't necessary, just to save a bit of typing. Whenever you concatenate the conversion number -> string is done automatically, and since they are always proper numbers, there's no need to quote them.

0
Doesn't work. I just won't make it, i'll think of a different plugin to make... Grenaderade 525 — 10y
0
Most likely your error is in using `GetObjects`, not in the random selection, since I didn't screw that up. BlueTaslem 18071 — 10y
Ad

Answer this question