✓ Accepted answer
I assume you would've got an error similar to this:
"bad argument #1 to 'random' (number expected, got nil)"
math.random(min,max)
can only be used on numerical values, not userdata objects. In order to select a random object from an array you'd have to specify a number range for the amount of objects in the table. Using #names will return how many objects are in the array, therefore
math.random(1,#names)
will return a number between 1 & 3, as you have 3 objects. This would work with any number of objects, for example if the array had 6 objects, repeating the same line of code would return a number between 1 & 6.
So now you have the position of the randomly selected object in the array, in order to retrieve the object from the array you would use
x=names[math.random(1,#names)]
So the final code would be,
local amy = script.Amy.BillboardGui
local sarah = script.Sarah.BillboardGui
local joey = script.Joey.BillboardGui
local names = {joey, sarah, amy}
game.Players.PlayerAdded:connect(function(plr)
x=names[math.random(1,#names)]
x.Parent = plr.Character:findFirstChild("Head")
end)
1 comment
Please tell us what you want to do. This script right now tells me NOTHING!