The script looks like this and it's a regular script, not module or local.
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 SpawnPoint = math.random(1,2) if SpawnPoint == 1 then print("1 spawn point.") char.Torso.CFrame = game.Workspace.Map1.Spawns.SpawnLocation1.CFrame print("Teleported.") if script.Configuration.SwordFighting.Value == true then local Sword = game.ServerStorage.ClassicSword:Clone() Sword.Parent = game.Players.LocalPlayer.Backpack end elseif SpawnPoint == 2 then print("2 spawn point.") char.Torso.CFrame = game.Workspace.Map1.Spawns.SpawnLocation2.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 end
The script works in studio, but it doesn't work in a public server. I was testing it with my best friend, trustycoocoolookcoo, and it didn't work... any ideas? When we are in the server, neither of us teleport to the map or get the swords, and are still stuck in the lobby.
First, let's clean up your code, then we'll fix the problem.
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() -- List of all Spawns 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
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
can't be nil
. This check just doesn't make sense. On a related note, you should just use workspace
instead of game.Workspace
.
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() -- List of all Spawns 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() -- in case they don't have a Backpack... use findfirstchild! 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
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.
is it in a local script?