Hi, I’m trying to make a random coin spawning system where coins will spawn in a certain area, but have different positions in that area. The problem is that whenever I collect a coin they will respawn in that same position they were before. Is there any way where I can make the coins respawn in random positions?
Here is the code inside ServerScriptService:
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local function collectCoin(coinMesh, rPosition)
coinMesh.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = players:GetPlayerFromCharacter(hit.Parent)
if player then
player.leaderstats.Stars.Value += coinMesh.Reward.Value
task.spawn(function()
coinMesh.Parent = replicatedStorage
task.wait(math.random(1,5))
local Pos = Vector3.new(rPosition.X, 3.75, rPosition.Z)
coinMesh.Position = Pos
coinMesh.Parent = workspace.Debris
end)
end
end
end)
end
local function spawnCoin(coinMesh, spawnArea)
local newCoin = coinMesh:Clone()
newCoin.Parent = workspace.Debris
local spacing = 2
local SpawnRandomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / spacing, spawnArea.Size.X / spacing), 3.75, math.random(-spawnArea.Size.Z / spacing, spawnArea.Size.Z / spacing))
local randomPosition = Vector3.new(SpawnRandomPosition.X, 3.75, SpawnRandomPosition.Z)
coinMesh.Position = randomPosition
collectCoin(newCoin, SpawnRandomPosition)
end
for _, spawnArea in workspace.CoinSpawns:GetChildren() do
for i = 1, 20, 1 do
spawnCoin(script.Star, spawnArea)
end
end
if you make a part that spreads across the area where you want them to spawn you can do this:
local spawnArea = workspace.part -- where you want them to spawn
local halfSize = spawnArea.Size/2
local randX = math.random(-halfSize.X, halfSize.X)
local randZ = math.random(-halfSize.Z, halfSize.Z)
local rCF = spawnArea.CFrame + Vector3.new(randX, 0, randZ)
-- random position in cframe form
since the coin would spawn in the middle of the spawn area if you set the cframe the same, you can halve the size of it and get the upper and lower boundaries of the part
additionally, if you want the areas that they spawn at to be spaced out evenly, you could do this:
local spawnArea = workspace.part
local halfSize = spawnArea.Size/2
local spacing = 1 -- how far apart you want them to be (in studs)
local spawnAreas = {}
for x = -halfSize.X, halfSize.X, spacing do -- get list of all possible spawns
for z = -halfSize.Z, halfSize.Z, spacing do
local cf = spawnArea.CFrame + Vector3.new(x, 0, z)
table.insert(spawnAreas, cf)
end
end
local randomCFrame = spawnAreas[math.random(1,#spawnAreas)]
-- choose from list randomly
initializing the list of spawn areas everytime you want to choose a spawn area for it would be inefficient so i’d recommend only running that once and just running the last line of code to pick a random spot
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local function collectCoin(coinMesh, rPosition, spawnArea)
coinMesh.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = players:GetPlayerFromCharacter(hit.Parent)
if player then
player.leaderstats.Stars.Value += coinMesh.Reward.Value
task.spawn(function()
coinMesh.Parent = replicatedStorage
task.wait(math.random(1,5))
local spacing = 2
local SpawnRandomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / spacing, spawnArea.Size.X / spacing), 3.75, math.random(-spawnArea.Size.Z / spacing, spawnArea.Size.Z / spacing))
local randomPosition = Vector3.new(SpawnRandomPosition.X, 3.75, SpawnRandomPosition.Z)
coinMesh.Position = randomPosition
coinMesh.Parent = workspace.Debris
end)
end
end
end)
end
local function spawnCoin(coinMesh, spawnArea)
local nSpawn = spawnArea
local newCoin = coinMesh:Clone()
newCoin.Parent = workspace.Debris
local spacing = 2
local SpawnRandomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / spacing, spawnArea.Size.X / spacing), 3.75, math.random(-spawnArea.Size.Z / spacing, spawnArea.Size.Z / spacing))
local randomPosition = Vector3.new(SpawnRandomPosition.X, 3.75, SpawnRandomPosition.Z)
coinMesh.Position = randomPosition
collectCoin(newCoin, SpawnRandomPosition, nSpawn)
end
for _, spawnArea in workspace.CoinSpawns:GetChildren() do
for i = 1, 20, 1 do
spawnCoin(script.Star, spawnArea)
end
end