How to get position of Player and make the entities spawn near the player

I’m trying to achieve where the entities will spawn near the player but far enough where the players can’t see.

function module:GetPlayersLocation()
	
	local Table = {}
	
	for _, Player in Players:GetPlayers() do
		
		if (not Player and Player.Character) then
			Player.CharacterAppearanceLoaded:Wait()
		end
		
		table.insert(Table, Player.Character:GetPivot())
		
	end
	
	return Table
	
end

function module:AddEntities(Entities, SizeTable, CanSpawnNearPlayer)

	if Spawned[Entities] == true then
		return
	end
	
	Spawned[Entities] = true
	
	if typeof(Entities) ~= "table" then
		error("Provide a table please!")
		return
	end
	
	if CanSpawnNearPlayer == true then
		
		local P_ = module:GetPlayersLocation()
		
		for i = 1, Entities.Count do

			local Randomize : Model = Entities.Table[math.random(1, #Entities.Table)]:Clone()

			Randomize.Parent = Entities.Parent

			Randomize:PivotTo(CFrame.new(
				math.random(-P_[i].Position.X * 2, P_[i].Position.X * 2), 
				workspace.Terrain.Size.Y + 125, 
				math.random(-P_[i].Position.Z * 2, P_[i].Position.Z * 2)
				)
			)

			if typeof(SizeTable) == "table" and SizeTable ~= nil then

				if math.random() * 100 <= SizeTable.Chance then

					Randomize:ScaleTo(SizeTable[1])

				else

					Randomize:ScaleTo(SizeTable[2])

				end

			end

			task.wait(.05)

		end
		
	else
2 Likes

maybe try spawning an invisible part and checking if the player can see it, and if not, you can spawn the entity
Detect if player is looking at object - #5 by sleitnick?