Spawn Script Not Working

Hello Developers! I am currently making a cooking minigame for the game I am making and it is not working. It gets the first printed message than it doesn’t print any of the other messages. Here is the script.

local replicatedStorage = game:GetService("ReplicatedStorage")
local Camera = workspace.CurrentCamera

replicatedStorage.StartCooking.OnClientEvent:Connect(function()
	print("Recived RemoteEvent")
	
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = workspace.WorkStation.CamPart.CFrame
	
	local JobStationItems = replicatedStorage:WaitForChild("JobStationItems")
	local Ingredients = workspace:WaitForChild("WorkStation"):WaitForChild("Ingredients")
	
	for i, slots in pairs(workspace.WorkStation:WaitForChild("Spawns"):GetChildren()) do
		print("Found parts!")
		if slots:IsA("BasePart") then
			
			print("Cloning Parts")
			local RandomItem = math.random(1, #Ingredients)
			local RandomItemClone = JobStationItems:FindFirstChild(Ingredients[RandomItem].Name):Clone()
			
			local slot = slots:GetChildren()
			
			RandomItemClone.Parent = Ingredients
			RandomItemClone.CFrame = CFrame.new(slot.CFrame)
		end
	end
end)
1 Like

Maybe the problem lies here, though I’m not sure.

EDIT: Using multiple WaitForChild is not necessary all the time. By the way, you can just store those variables outside the OnClientEvent.

Final code:

local replicatedStorage = game:GetService("ReplicatedStorage")

local cookingEvent = replicatedStorage.StartCooking
local JobStationItems = replicatedStorage:WaitForChild("JobStationItems")
local Workstation = workspace:WaitForChild("WorkStation")

local Ingredients = Workstation.Ingredients
local Spawns = Workstation.Spawns

local Camera = workspace.CurrentCamera

cookingEvent.OnClientEvent:Connect(function()
	print("Received RemoteEvent")
	
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = WorkStation.CamPart.CFrame
	
	for i, slots in Spawns:GetChildren() do
		print("Found parts!")

		if slots:IsA("BasePart") then
			
			print("Cloning Parts")
			local RandomItem = math.random(1, #Ingredients)
			local RandomItemClone = JobStationItems:FindFirstChild(Ingredients[RandomItem].Name):Clone()
			
			local slot = slots:GetChildren()
			
			RandomItemClone.Parent = Ingredients
			RandomItemClone.CFrame = CFrame.new(slot.CFrame)
		end
	end
end)

It still doesn’t work. It just prints the “Received RemoteEvent”.

1 Like

Make sure Spawns:GetChildren() returns a table filled up with items. if #items == 0, the loop won’t run

EDIT: Try storing the items in a variable and printing the table.

local SpawnsObjects = Spawns:GetChildren()

print(SpawnsObjects, #SpawnsObjects)

It says {} 0

REMOVE30CHARACTERLIMITBRO

This print means there are no items in Spawns table, meaning the loop will never run, as there are no items inside whatsoever. The problem is probably because the Spawns is not replicating to server, only for client, but that’s only my say.

I’ll change it to the server rq

1 Like

It still doesn’t work for some reason

local Station = script.Parent.Parent.Parent
local debounce = false

local replicatedStorage = game:GetService("ReplicatedStorage")

local JobStationItems = replicatedStorage:WaitForChild("JobStationItems")
local Workstation = workspace:WaitForChild("WorkStation")

local Ingredients = Workstation.Ingredients
local Spawns = Workstation.Spawns


script.Parent.Triggered:Connect(function(player)
	if debounce then return end
	debounce = true

	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then debounce = false return end

	local otherValues = player:FindFirstChild("OtherValues")
	if otherValues and otherValues:FindFirstChild("JobValue") and otherValues.JobValue.Value == "Chef" then
		humanoid.WalkSpeed = 0
		humanoid.JumpHeight = 0

		
		script.Parent.Enabled = false

		-- Teleport player
		character:WaitForChild("HumanoidRootPart").CFrame = Station:WaitForChild("PlayerSpawn").CFrame

		game.ReplicatedStorage:WaitForChild("StartCooking"):FireClient(player)
		
		for i, slots in Spawns:GetChildren() do
			print("Found parts!")

			if slots:IsA("BasePart") then

				print("Cloning Parts")
				local RandomItem = math.random(1, #Ingredients)
				local RandomItemClone = JobStationItems:FindFirstChild(Ingredients[RandomItem].Name):Clone()

				local slot = slots:GetChildren()

				RandomItemClone.Parent = Ingredients
				RandomItemClone.CFrame = CFrame.new(slot.CFrame)
			end
		end
	end

	debounce = false
end)

Are you sure you are creating the spawns in the server?

for i, slots in pairs(workspace.WorkStation:WaitForChild("Spawns"):GetChildren()) do

change that to:

for i, slots in pairs(workspace.WorkStation:FindFirstChild("Spawns"):GetChildren()) do