First, let's clean up your code, then we'll fix the problem.
Repeated Code
Big problem: repeated code. You repeat this code twice (plus most of the lines before it):
print("Teleported.")
if script.Configuration.SwordFighting.Value == true then
local Sword = game.ServerStorage.ClassicSword:Clone()
Sword.Parent = game.Players.LocalPlayer.Backpack
end
There's no reason to have it in each branch --- just do it once after.
The remaining if is still pretty repetitious. If you just want to pick a random spawn, why not do that explicitly?
if game ~= nil then
wait(2.5)
for i, v in pairs(game.Players:GetChildren()) do
local char = game.Players.LocalPlayer.Character
if char.Torso ~= nil then
local spawns = workspace.Map1.Spawns:GetChildren()
local spawn = spawns[math.random(#spawns)]
char.Torso.CFrame = spawn.CFrame
print("Teleported.")
if script.Configuration.SwordFighting.Value == true then
local Sword = game.ServerStorage.ClassicSword:Clone()
Sword.Parent = game.Players.LocalPlayer.Backpack
end
end
end
end
char.Torso ~= nil
You can't check for children like that. If there isn't a Torso, you get an error, not the value nil back. Use :FindFirstChild to check if there's a thing. Also, the ~= nil is redundant, and often just adds extra clutter.
== true is even more redundant than ~= nil.
game ~= nil
game can't be nil. This check just doesn't make sense. On a related note, you should just use workspace instead of game.Workspace.
for i, v in pairs(game.Players:GetChildren()) do
First, note that you should use :GetPlayers() instead of :GetChildren() to get a list of players. The difference is small, but that method exists for a reason.
You don't use either i or v in your code. This is a massive red flag. If you are defining variables but not using them, it means you're using something else.
In this case, it's pretty clear where that is.
LocalPlayer can only be used from a LocalScript. Even if it did work in a Server script, why would you want to use the same player over and over? v is the player that you want! You should use a better variable name, though:
wait(2.5)
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
if char:FindFirstChild("Torso") then
local spawns = workspace.Map1.Spawns:GetChildren()
local spawn = spawns[math.random(#spawns)]
char.Torso.CFrame = spawn.CFrame
print("Teleported.")
if script.Configuration.SwordFighting.Value then
local Sword = game.ServerStorage.ClassicSword:Clone()
Sword.Parent = player:FindFirstChild("Backpack")
end
end
end
There's one small thing more -- a .Character can be nil before they spawn for the first time. We should make sure char exists, too:
local char = player.Character
if char and char:FindFirstChild("Torso") then
Timing
One final problem. This script will run exactly once, and it will be precisely two and a half seconds after the server starts.
Chances are, not even on player has connected by that time -- definitely not two.
You'll want to choose more wisely when this script runs. By an event, or in a loop, periodically.
3 comments
Could you elaborate on the problem? Also, make your title relevant to the question please.
Is this the whole script?
Yes, that is the whole script.