Random spawn not working

I’m trying to make a script to spawn a ball every second at 1 of 3 spawn positions by making the ball’s CFrame the CFrame of the spawn. However, whenever I run it, I get the error ServerScriptService.SpawnBall:13: attempt to index number with ‘CFrame’. Here’s my script:

local spawns = {
	game.Workspace.BallSpawn1,
	game.Workspace.BallSpawn2,
	game.Workspace.BallSpawn3,
}

while wait(1) do
	local pos = math.random(1,#spawns)
	print(pos)
	
	local ball = game.ReplicatedStorage.Ball:Clone()
	ball.Parent = game.Workspace
	ball.CFrame = pos.CFrame
end
1 Like

You have generated a random number, but did not actually index the spawn and hence the CFrame property in your table.

local spawns = {
	game.Workspace.BallSpawn1,
	game.Workspace.BallSpawn2,
	game.Workspace.BallSpawn3,
}

while wait(1) do
	local ran = math.random(1, #spawns)
    local pos = spawns[ran]
	
	local ball = game.ReplicatedStorage.Ball:Clone()
	ball.Parent = game.Workspace
	ball.CFrame = pos.CFrame
end
1 Like