Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When I have multiple people, they don't teleport or get the weapon, why does this happen?[ANSWERED]

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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.

0
Could you elaborate on the problem? Also, make your title relevant to the question please. Goulstem 8144 — 9y
0
Is this the whole script? Goulstem 8144 — 9y
0
Yes, that is the whole script. connieoop 10 — 9y

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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() -- 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

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() -- 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

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.

0
Thank you for doing that, when I am done with my school work, I will test this out. connieoop 10 — 9y
0
Also, About the timing, I only have the first map, so when I have multiple maps, I'll disable the map scripts, and have a script that randomly activates one. connieoop 10 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

is it in a local script?

0
No, it's not. connieoop 10 — 9y
1
Please don't answer with a question...use a comment. Muoshuu 580 — 9y

Answer this question