Sorry my experience is mediocre at best, not entirely sure how this should piece together for my intended outcome.
What I want to do is set multiple MonsterSpawners in different areas and run all of their location checks/spawn commands from a single module. Each spawner will only populate a specific type of enemy and are designated in specific folders as seen in the code below.
[Disclaimer - it’s incomplete/untested, just sort of putting the pieces together to visualize as I go on how best to handle it.]
-- Monster Spawner Template - Brainstorm v.1
local Player = game.Players.LocalPlayer
local Part = Instance.new("Part")
local TS = game:GetService("TweenService")
local RS = game:GetService("ReplicatedStorage")
local module = {
function findClosestPlayer(spawner, targetedDistance)
local closestDistance
local closestChar
for i, player in pairs(game.Players:GetPlayers()) do
local character = player.Character
local vector = (character.HumanoidRootPart.Position - spawner.Position)
if not closestDistance then
closestDistance = vector.Magnitude
closestChar = character
else
if vector.Magnitude < closestDistance then
closestDistance = vector.Magnitude
closestChar = character
end
end
end
if closestDistance > targetedDistance then return end
return closestDistance, closestChar
end
game:GetService("RunService").Heartbeat:Connect(function() --Handle all spawners of a specific monster
local spawners = {
game.Workspace.MonsterSpawners.SlimeOne.spawner1,
game.Workspace.MonsterSpawners.SlimeOne.spawner2,
game.Workspace.MonsterSpawners.SlimeOne.spawner3,
}
-- ToDo determine how to specify which Monster will spawn based on distance from Specific Name of Spawner
-- Maybe something like...
coroutine.wrap(function()
while task.wait() do
for _,v:Instance? in workspace.MonsterSpawners:GetChildren() do
if v.Name:find("spawner") and v:IsA("BasePart") then
if Distance(Character, v) then
-- Hmm, possible to return childs name if matching target range and set a unique variable
-- based on the specific spawner
end
end
end
end
end)()
-- Individual Spawner Commands?..
for i, v in ipairs(spawners) do
local targetedRange = 50
for i, spawner in pairs(spawners) do
local closestDistance, closestCharacter = findClosestPlayer(spawner, targetedRange)
if closestDistance and closestCharacter and (Unique Variable from above) then
-- bunch of else commands best way to check for each unique monster variable and send spawn command only to that spawner?
local Monster = Instance.new(game.WorkSpace.Monsters.Slimes.SlimeOne:GetChildren())
Monster.Parent = game.Workspace
Monster.Name = "SlimeOne"
Monster.Position = spawner.Position, Vector3.new()--ToDo determine spawn range random near spawner & check collision)
game.Workspace:AddItem(Monster,math.random(1, 5)) -- Amount of Monsters to Spawn in one check
-- Set delay before running spawn routine again, possible to check via monster death in location range?
end
end
end
end)
}
return module
Advice, comments, better suggestions?