How loop to through a folder and add an item to each individual value in that folder

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Im am trying to make a working griddle stove, but I’ve stumbled upon a problem as per the video below I want it so when the patty is clicked it spawns on top of the grill where the red blocks are located. It works but only spawns at one of the red box. I also notice that when I set the attribute back to true it sets all of the values attribute to true.

  2. What is the issue? Include screenshots / videos if possible!
    robloxapp-20230321-1504022.wmv (1.1 MB)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    So far only solutions I have done is tried to switch the code around to see if change, but I feel it something minimal. I just can’t see it.

local griddle = workspace.Griddle
local ingredients = workspace.Ingredients -- Folder for the patties
local FoodPlaceHolderArray = griddle.FoodPlaceHolder -- Folder for the redboxes
local SoundService = game:GetService("SoundService")
local frying = SoundService.GrillFeedBackSounds.Frying


-- This function teleports the patty to the grill once the Patty is clicked on.
local function sendPattyToGrill(object) 
	for _, placeHolder in pairs(FoodPlaceHolderArray:GetChildren())do
		if placeHolder:GetAttribute("Occupied") == false then -- Checks if the redbox is occupied
			object.Position = placeHolder.Position -- teleport patty to the space
			placeHolder:SetAttribute("Occupied", true)  --
		end
		frying:Play()
		placeHolder:SetAttribute("Occupied", false)
	end
	
end

for _, v in pairs(ingredients:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function()
		sendPattyToGrill(v)
	end)
end


the Sound:Play() function is not a yielding function. Aka, it doesn’t hold up the script till it finishes. Just like if you play a tween, it’ll automatically move onto the next line.

So what’s happening in your script, is it’s playing the sound, and then going to the next line immediately, and changing the occupied value to false.

If you want to fix this, you can use the frying.Ended event.

frying:Play()
frying.Ended:Wait() -- :Wait() is an inherited method from the class RBXScriptSignal

An issue you’ll likely encounter though, is that if you play it several times, the ended event will be the same for each burger.

If that doesn’t make sense, feel free to ask clarifying questions

1 Like

Ohh, Thank you that fixed the problem.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.