How can i make this in my code?

Ive made a system whereas a certain amount of coins randomly spawn in specific areas, i already have this part covered but i need help on the other part of the system. How can i make it so it checks each certain period of time if there is a place for where the coins can spawn and the local function activates? Since ive made a script where its spawn areas constantly change and i want it different for each change, and in this script the local function activates when the server first loads in or when the game begins, i want it to have a check if theres an available spawn point for it and the local function activates.
This is the script for the coins spawning


It may seem a bit confusing so please correct me if i have to provide more information :pray:

2 Likes

It’s kind of confusing to read… Are you saying you want the coins to spawn randomly without needing the for loop? Or are you saying you want the coins to respwan after a while when they get picked up?

1 Like

not like that, i already have a script for the coins spawning randomly but what it needs is a part it spawns randomly around, i made a script for that parts position being changed consistently in a certain amount of time, and for the script i made the coins spawn when the server starts (or when i press play in studio) when it looks for a part to spawn randomly around, and i want to make it so it checks wether there is an available part inside that folder and the spawn function activates, and by my script i made the part get destroyed after a designated period of time, so the previous part it spawns around dissapears and a new one comes in, so i want it to check if theres a new one each time the old one gets destroyed then the spawn function activates, think of it like a map changing script and i want to check what new map it is and spawn coins according to its terrain

So If I’m understanding right… You want to loop through the folder to check for parts/spwan points that don’t already have a coin on them right? And If there’s a coin you destroy the previous one?

yes i want to loop through the
folder and make it repeat constantly to check if theres available parts for the coins to spawn around but i dont want them to destroy that coin area as i already have an other script for it, the purpose of this script it to just make the coins spawn in those areas when it checks theres an available area to spawn in
Screenshot 2024-08-05 071515
this is where the folders will go in
Screenshot 2024-08-05 071536
and inside these folders are parts where the coins are able to randomly spawn around, each few minutes the folders in coinsholder who have parts inside will be destroyed and a new area will be made in a different position, so the coin script checks if theres another available part to spawn around and then the spawn function activates which spawns the coins in those areas, and i want this loop to be able to be repeated constantly

This is quite easy when I understand what you want.
I’ll explain it to you in steps, not spoon-feed you code

  1. Loop through your folder to access the parts that the coins spawn within.
  2. Use workspace:GetPartBoundsInBox() to check for parts within the part.
  3. Loop through the table returned and check if there is a coin in there.
  4. If the coin is in there, you continue checking other parts. If there are no coins within the part then spawn your coin.

Heres example code for a thing I made to check if spots were filled or not:

local GetAvailableSpots = function() : {BasePart}
	local Spots = {}
	
	for I, X in pairs(SpwanPoints:GetChildren()) do
		if not X:IsA("BasePart") then continue end 
		
		local Params = OverlapParams.new()
		Params.FilterDescendantsInstances = {workspace.SpecialRigs}
		Params.MaxParts = 9
		Params.FilterType = Enum.RaycastFilterType.Exclude
		
		local Cast = workspace:GetPartBoundsInBox(X.CFrame, X.Size, Params)
		local SpotOpen = true
		
		for II, XX in pairs(Cast) do
			local parent = XX.Parent
			
			if parent.Name == "Dummy" and parent:FindFirstChildOfClass("Humanoid") and parent.Parent == DummiesFolder then SpotOpen = false break end
		end
		
		if SpotOpen then 
			table.insert(Spots, X)
		end
	end
	
	return Spots
end

apologies for the late responses, Thats kind of what i want but ive already covered some of the parts of your explanation in my script, the only thing im having trouble with is finding out how to repeat this


each certain periods of time like maybe each 3 minutes or 2 minutes or 40 seconds or any designated time, just like a repeat until loop but the loop doesnt stop and repeats forever and the loop is repeated again each certain designated period of time, except i dont really know how to do it since im still fairly new to roblox lua coding

Not sure if it’s the best way to do it, but you could set up a RunService.Stepped to check the last time coins were spawned. You could save the time in a variable like local time = tick(), and then check the time since the last round of coins spawning with if tick() - time > cooldown then, with the cooldown being however many seconds you want to wait between coins being spawned.

Honestly you could also just do:

while task.wait(cooldown) do
    -- Your loop here
end

But again, not sure if that would be a good way of doing it. It should get the job done though.

Sorry if I misunderstood what you wanted.

1 Like