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

How do I work with math.random??

Asked by
Hasburo 150
8 years ago
Edited 8 years ago

The purpose of this script is for every five seconds, it calls on a function which should randomly select a chest/part out of a model, and then it should randomly pick a brick out of a model which sets the chest's position to the randomly selected brick.

Basically, every 5 seconds, a random chest/part spawns at a random brick's position. Except it's not working, and it's been erroring with this;

21:57:38.685 - Workspace.TemplateLoot.LootScript:5: attempt to get length of global 'ChestMeshes' (a userdata value)

Here's the script;

ChestMeshes = game.ReplicatedStorage:FindFirstChild("Chests")
ChestSpawns = game.Workspace:FindFirstChild("ChestSpawns")

local function spawnChest()
local newChest = ChestMeshes[math.random(1,#ChestMeshes)]:Clone()
return newChest
end


local function randomSpawn()
local randomSpawn = ChestSpawns[math.random(1,#ChestSpawns)]
return randomSpawn
end


local function final()
local newChest = spawnChest()
local randomSpawn = randomSpawn()
newChest.Position = randomSpawn.Position
end


while true do
wait(5)
final()
end

1 answer

Log in to vote
0
Answered by 8 years ago

ChestMeshes and ChestSpawns are not a table. You have to make them into tables before choosing a random one.

ChestMeshes = game.ReplicatedStorage:FindFirstChild("Chests"):GetChildren() -- the problem
ChestSpawns = game.Workspace:FindFirstChild("ChestSpawns"):GetChildren()

local function spawnChest()
local newChest = ChestMeshes[math.random(1,#ChestMeshes)]:Clone()
return newChest
end


local function randomSpawn()
local randomSpawn = ChestSpawns[math.random(1,#ChestSpawns)]
return randomSpawn
end


local function final()
local newChest = spawnChest()
local randomSpawn = randomSpawn()
newChest.Position = randomSpawn.Position
end


while true do
wait(5)
final()
end
Ad

Answer this question