Attempt to get length of instance value (Spawner I am making

I am making a system where a strawberry bush spawns strawberries every 5-10 seconds. I keep getting errors and I feel like I might not understand something. Thank you for your help!

-- Services



-- Variables

local Bush = script.Parent.Parent
local StrawberrySpawns = Bush.StrawberrySpawns
local Strawberries = game.ReplicatedStorage.Berries.Strawberry

for _, spawner in pairs(StrawberrySpawns:GetChildren()) do
	local RandomSpawnTime = math.random(5,10)
	task.wait(RandomSpawnTime)
	
	local SpawnStrawberries = Strawberries[math.random(1, #Strawberries)]
	
	SpawnStrawberries.CFrame =  spawner.CFrame
	SpawnStrawberries.Parent = workspace.StrawberrySpawned

end

Thank you for your time!

This is the problem, but Iā€™m confused about what you are trying to do here, can you explain and show the Strawberries thing in explorer?

Strawberries are in replicated storage because they need to be cloned and then put in workspace and then have its position to the StrawberrySpawns in a folder, StrawberrySpawned is an empty folder for the strawberries to go in.

1 Like

What is the error. try this but if it doesnt work just tell me the error:

local Bush = script.Parent.Parent
local StrawberrySpawns = Bush.StrawberrySpawns
local Strawberries = game.ReplicatedStorage.Berries.Strawberry

for _, spawner in pairs(StrawberrySpawns:GetChildren()) do
	task.wait(math.random(5,10))
	
	local SpawnStrawberries = Strawberries[math.random(1, #Strawberries)]
	
	SpawnStrawberries.CFrame =  spawner.CFrame
	SpawnStrawberries.Parent = workspace.StrawberrySpawned

end

the error is attempt to get length of a instance value

U forgot to use :GetChildren()

local Bush = script.Parent.Parent
local StrawberrySpawns = Bush.StrawberrySpawns
local Strawberries = game.ReplicatedStorage.Berries.Strawberry:GetChildren()

for _, spawner in pairs(StrawberrySpawns:GetChildren()) do
	task.wait(math.random(5,10))
	
	local SpawnStrawberries = Strawberries[math.random(1, #Strawberries)]
	
	SpawnStrawberries.CFrame =  spawner.CFrame
	SpawnStrawberries.Parent = workspace.StrawberrySpawned

end

Uhm this is wrong, you need to use seperate things for this

local StrawberryItem = game.ReplicatedStorage.Berries.Strawberry
local Strawberries = StrawberryItem:GetChildren()
....
local SpawnStrawberries = StrawberryItem[math.random(1,#Strawberries)]
local function pickRandomStrawberry(strawberries: Instance): Instance?
	local children = strawberries:GetChildren()
	return children[math.random(1, #children)]
end

--in your loop:
local SpawnStrawberries = pickRandomStrawberry(Strawberries)

My bad, u can shorten it:

local Bush = script.Parent.Parent
local StrawberrySpawns = Bush.StrawberrySpawns
local Strawberries = game.ReplicatedStorage.Berries.Strawberry

for _, spawner in pairs(StrawberrySpawns:GetChildren()) do
	task.wait(math.random(5,10))
	
	local SpawnStrawberries = Strawberries[math.random(1, #Strawberries:GetChildren())]
	
	SpawnStrawberries.CFrame =  spawner.CFrame
	SpawnStrawberries.Parent = workspace.StrawberrySpawned

end
1 Like

im so confused which one do I use

local Bush = script.Parent.Parent
local StrawberrySpawns = Bush.StrawberrySpawns
local Strawberries = game.ReplicatedStorage.Berries.Strawberry

for _, spawner in pairs(StrawberrySpawns:GetChildren()) do
	task.wait(math.random(5,10))
	
	local SpawnStrawberries = Strawberries[math.random(1, #Strawberries:GetChildren())]
	
	SpawnStrawberries.CFrame =  spawner.CFrame
	SpawnStrawberries.Parent = workspace.StrawberrySpawned

end

Workspace.StrawberryBush.Mechanics.StrawberrySpawner:8: invalid argument #2 to ā€˜randomā€™ (interval is empty) is the new error when I c + p that in

local Bush = script.Parent.Parent
local StrawberrySpawns = Bush.StrawberrySpawns
local Strawberries = game.ReplicatedStorage.Berries

for _, spawner in pairs(StrawberrySpawns:GetChildren()) do
	task.wait(math.random(5,10))
	
	local SpawnStrawberries = Strawberries[math.random(1, #Strawberries:GetChildren())]
	
	SpawnStrawberries.CFrame =  spawner.CFrame
	SpawnStrawberries.Parent = workspace.StrawberrySpawned

end

3 is not a valid member of Folder ā€œReplicatedStorage.Berriesā€

Change it to this Strawberries:GetChildren()[math.random(1,#Strawberries:GetChildren())]

1 Like

replace this
local SpawnStrawberries = Strawberries[math.random(1, #Strawberries)]
with this
local SpawnStrawberries = Strawberries[math.random(1, #Strawberries:GetChildren())]

same errorrrrrrrrrrrrrrrrrrrrrrrrrr

try this This text will be blurred

I fixed it and how I did was I numbered all the strawberries 1-5 and it worked. It isnt very convenient tho because I want it tobe called strawberry not ā€œ1ā€ but yeah.