Hello. I have tried to make a script that spawns the player’s character once a server event is fired and it only works until line 23, where it attempts to “compare number < nil”. I would like to know how I could get this script to work as intended, since it’s getting a bit confusing even for me.
Below is the code I have written:
events.Spawn.OnServerEvent:Connect(function(player)
player:LoadCharacter()
events.Spawn:FireClient(player)
local char = player.Character or player.CharacterAdded:Wait()
local spawns = workspace:WaitForChild("Map"):WaitForChild("Spawns")
local closestmagnitude = math.inf
local closestspawn = nil
for i, spawnloc in pairs(spawns:GetChildren()) do
if spawnloc:IsA("SpawnLocation") then
for i, enemy in pairs(workspace:GetChildren()) do
if players:GetPlayerFromCharacter(enemy) then
local ep = players:GetPlayerFromCharacter(enemy)
if ep.TeamColor ~= player.TeamColor then
local distance = (enemy:FindFirstChildOfClass("Humanoid").RootPart.Position - spawnloc.Position).Magnitude
print(enemy.Name.." is "..distance.." studs away from this spawn")
if distance < closestmagnitude then
closestmagnitude = distance
closestspawn = spawnloc
print(closestspawn)
char:MoveTo(closestspawn.Position)
end
end
end
end
end
end
end)
As you can see, it goes through the available spawns, compares the distance between enemy players and the spawns, and attempts to select the spawn that is farthest from an enemy player character so it can teleport the player there after loading their character. But the problem lies in the “select the spawn” part.
I managed to solve it by replacing “if distance < closestmagnitude then” with “if distance > closestmagnitude then” and replacing “local closestmagnitude = math.inf” with “local closestmagnitude = 1”. I am, however, open to suggestions in case there’s a better way of doing it, or ways to improve the code.
Glad your issue is solved, but, I would recommend cleaning your code up by replacing all nested if statements and for loops with the continue method like so:
for i, spawnloc in pairs(spawns:GetChildren()) do
if not spawnloc:IsA("SpawnLocation") then continue end
This is just an example but if you do this for almost all the statements then it would be pretty neat.
I would also recommend using full names for variables so rather than using spawnloc use spawnLocation.
math.inf doesn’t exist and running it would just return nil. If you want to know why it won’t error then consider the math method as a table with various functions so if you try to call something that doesn’t exist in the table it would return nil.
OP probably originally tried using math.huge but may have forgotten the exact word for it.
I would recommend cleaning your code up by replacing all nested if statements and for loops with the continue method like so:
Is there a performance benefit in using this method?
OP probably originally tried using math.huge but may have forgotten the exact word for it.
That is true. I got it mixed up. However, math.huge wouldn’t have been the right choice anyway so I went for 1 because I need a small number rather than infinite.