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
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.
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
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)
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
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
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
replace this local SpawnStrawberries = Strawberries[math.random(1, #Strawberries)]
with this local SpawnStrawberries = Strawberries[math.random(1, #Strawberries:GetChildren())]
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.