Spawning items in different spots everytime

So I have a simple script that spawns marbles into select spots. The only issue is that it will spawn some marbles in places where a marble has already been spawned. I hope someone knows :man_shrugging:

local Spawners = workspace["Giant Plinko"].MarbleSpawnLocations:GetChildren()

local marble = game.ServerStorage:WaitForChild("Marble")

function SpawnMarb()
	local NewCoin = marble:Clone()
	local MarbleSpawn = Spawners[math.random(1, #Spawners)]
		
	end

	NewCoin.CFrame = MarbleSpawn.CFrame
	NewCoin.Anchored = true
	NewCoin.Parent = workspace
end

while true do
	SpawnMarb()

	wait(.05)
end

The only solution I could think of would be to check if there is already a marble there but I dont know that much.

1 Like
local Spawners = {}
local marble = game.ServerStorage:WaitForChild("Marble")

function SpawnMarb()
	local NewCoin = marble:Clone()
	if #Spawners > 0 then
		local MarbleSpawn = table.remove(Spawners, math.random(1, #Spawners))
		NewCoin.CFrame = MarbleSpawn.CFrame
		NewCoin.Anchored = true
		NewCoin.Parent = workspace
	else
		for _, v in pairs(workspace["Giant Plinko"].MarbleSpawnLocations:GetChildren()) do
			table.insert(Spawners, v)
		end
		SpawnMarb()
	end
end

while wait(0.017) do --minimum wait time (change if necessary)
	SpawnMarb()
end

May need some slight tweaking to fit your game exactly but all in all this should work.

1 Like

You can use this logic to check if a marble has already been spawned there.

local Spawners = workspace["Giant Plinko"].MarbleSpawnLocations:GetChildren()
local marble = game.ServerStorage:WaitForChild("Marble")

local function spawnerAvailable(s)
	return s:FindFirstChild("Marble") and false or true
end


local function SpawnMarb()
	local NewCoin = marble:Clone()
	local MarbleSpawn
        local function defSpawn()
                MarbleSpawn = Spawners[math.random(1, #Spawners)]
        end
        repeat
                defSpawn()
        until
                spawnerAvailable(MarbleSpawn)

	NewCoin.CFrame = MarbleSpawn.CFrame
	NewCoin.Anchored = true
	NewCoin.Parent = MarbleSpawn
end

game:GetService("RunService").Heartbeat:Connect(SpawnMarb)

Considering his existing code structure, this would NOT be a good way to achieve his goal. Take another look. Notice how he already has all of the spawns inside of a table. Instead of taking an extra step, lets just compare the potential spawn locations to where we know we have spawned marbles.

This new edit works, thanks! I will remember your name, you a real one.

Thank you for trying m8, means a lot someone would spend their time to help a noob like myself.

1 Like