Hey! I am currently trying to make an RPG Spawner for a Doomspire game, basically It decides where a player should spawn depending on some situations.
I currently need players to spawn on whatever spawnpoint is closer to an enemy on your base, my current script is super unreliable and constantly fails for whatever reason, it also sometimes chooses spawnpoints on doomspire floors under the player (not where the player is)
Could anyone help me solve my issues?
My current script:
function findclosestspawntoplayer(player, char, teamval)
local maxmagnitude = 23 -- set your desired maximum magnitude between a spawn and a player to define it as the closest
if player and teamval then
local teamtoignore = teamval
local closest
local PlayerPosition
if player.Character then
PlayerPosition = char.HumanoidRootPart.Position
end
for x, val in pairs(workspace.Doomspires:GetChildren()) do
if val:IsA("Model") and val.Name ~= teamtoignore then
local playernotfound = true
PlayerPosition = char.HumanoidRootPart.Position --setting the player's position again just in case
for i,v in pairs(val.Spawns:GetChildren()) do
if closest == nil then
closest = v
else
print((PlayerPosition - v.Position).magnitude)
if (PlayerPosition - v.Position).magnitude and (PlayerPosition - v.Position).magnitude <= maxmagnitude then
-- if the mag between the player position and the spawner position is lower than the closest spawner pos (if set) and lower than the max magnitude then it's defined as the new closest spawn
local results = GetTouchingParts(v)
for _, value in pairs(results) do
if value.Name == "Hitbox" then
if value.Team.Value == val.Name then
val.ChosenSpawn.Value = v
warn("set spawn from findclosestspawntoenemy (Priority 1)")
closest = v
playernotfound = false
end
end
end
end
end
end
warn(tostring(playernotfound).."playernotfound")
task.wait(.1)
if playernotfound == true then
--priority #2
checkifspawnisinenemybase(val.Name)
print("fired checkifenemybase")
end
end
end
end
end
Thanks for helping!