My coin spawner won't spawn coins in random positions, I need help to figure this out!

My part Spawner Script:

local MaxCoins = script.Parent.Max

local currentCoins = script.Parent.Current

local spawner = script.Parent.CoinsSpawner

local pos = Vector3.new(spawner.Position.X/2 , 2, spawner.Position.Z/2)

local canSpawn = true

while true do
	wait(1)
	if canSpawn == true then
		local coin = game.ReplicatedStorage.Coins:WaitForChild("Coin"):Clone()
		
		coin.Parent = game.Workspace.Area1

		coin.Union.Position = Vector3.new(pos)
		
		coin.Base.Position = coin.Union.Position
		coin.s.Position = coin.Base.Position
		
		currentCoins.Value += 1

		if currentCoins.Value == MaxCoins.Value then
			canSpawn = false
		end
	end
	
	if canSpawn == false then
		if currentCoins.Value < MaxCoins.Value then
			canSpawn = true
		end
	end
	
end

My video that shows it won

robloxapp-20230516-1237340.wmv (3.9 MB)

spawn part:

You’re not actually randomizing the position of your coins. You’re giving it the same position each time, which itself isn’t randomized either.

local MaxCoins = script.Parent.Max

local currentCoins = script.Parent.Current

local spawner = script.Parent.CoinsSpawner

local centerPos = Vector3.new(spawner.Position.X, spawner.Position.Y + 2, spawner.Position.Z)

local canSpawn = true

local function getRandomMultiplier()
	return math.random(-50, 50) / 100-- gives a random number between -0.5 and 0.5
end

while true do
	wait(1)
	if canSpawn == true then
		local coin = game.ReplicatedStorage.Coins:WaitForChild("Coin"):Clone()
		
		coin.Parent = game.Workspace.Area1


		local pos = centerPos + Vector3.new(spawner.Size.X * getRandomMultiplier(), 0, spawner.Size.Z * getRandomMultiplier())
		
		coin.Union.Position = Vector3.new(pos)
		
		coin.Base.Position = coin.Union.Position
		coin.s.Position = coin.Base.Position
		
		currentCoins.Value += 1

		if currentCoins.Value == MaxCoins.Value then
			canSpawn = false
		end
	end
	
	if canSpawn == false then
		if currentCoins.Value < MaxCoins.Value then
			canSpawn = true
		end
	end
	
end

Haven’t actually tested if this works but I’m like 80% sure it should

1 Like

It works, but how do I put the models position instead of adding the extra parts like the
"
coin.Union.Position = Vector3.new(pos)
coin.Base.Position = coin.Union.Position
coin.s.Position = coin.Base.Position
"
?

the model :
CaptureRoblox

Try this

coin:PivotTo(CFrame.new(pos))

It works exaclty how I wanted it, Thank you!

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