How Would I Make My Coin Script Spawn Random Coins Within A certain Radius?

I want my coins to spawn randomly around a map but only in a certain radius from the center point.
I’ve tried to make it work but I’m not good at this stuff so any help would be appreciated :smiley:

Script below

local Coins = game.Workspace.Coins
local CoinTemplate = script.Coin


local SpawnCoins = function()
	
	if game.ServerStorage.Values.RoundActive.Value == true then
		if #game.Workspace.Coins:GetChildren() < 25 then
			if game.Workspace:FindFirstChild("Map") then
				local Center = game.Workspace:FindFirstChild("Map").CoinParams.Center
				local CoinClone = CoinTemplate:Clone()
				local radius = 100

				local x = math.random(Center.Position.X,radius) 
				local y = Center.Position.Y
				local z = math.random(Center.Position.Z,radius) 
				CoinClone.Position = Vector3.new(x,y,z)
				CoinClone.Parent = Coins
			end
			
		end
	end
	
end

If you need more info Just ask

Why not use zone plus? You can even make the spawn not just circular.

7ff28d3f59653604557c8c51bac449399effb478

3 Likes

I would be taking a plain surface for this case. Have an arbitrary point rcos(theta), rsin(theta). Here r is the distance from the center and theta defines the position of point within that circle. Randomize the values if “theta” and “r”. And with non uniform terrain space, I would do the same thing except check every time a coin is spawned to see if the line of path between the coin and the player or a part is not obstructed by using ray cast. If obstructed don’t spawn in the coin otherwise spawn it in. But this will only spawn in coins on x-y plane and wont be able to spawn coins in z plane. So to overcome that, when there is path obstruction between coin and the part or player, check at which value of z there is no obstruction and then spawn in the coin. But considering it will have to check every z value which isn’t good for the game, I would go about increasing z value (maybe by 5 ) after every check and do the ray casting test again.

1 Like

Wow! didn’t know this existed, thanks :smiley: