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

Teleporting All Players with CFrame??

Asked by 10 years ago

Here is my script to teleport all players to a certain location.

game.Players:GetPlayers().CFrame = game.Workspace.NPC.Harold.Head.CFrame

Doesnt work, doesnt do anything, nothing in output, but the script continues after it instead of breaking

2 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

GetPlayers() returns a table, and tables don't have a CFrame property. You need to get the objects stored in that table to access their properties. We therefore must use a for loop.

Your second problem is that Players don't have a CFrame property either. We must get their character's torso, and set the CFrame of that.

--pairs() is just a function that is used to loop through a table. Value is the current value, or the player we are currently at. Index is the current index, but we won't be using it. 
for index,value in pairs(game.Players:GetPlayers()) do
    value.Character.Torso.CFrame = CFrame.new(0,0,0)
end

Just remember that this will throw an error if Character or Torso doesn't exist, so you may want to add in some extra checks in the future.

Ad
Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
10 years ago

Hello, that line of code wont work, and shouldn't be working. You're creating a table then CFraming ithe table to a CFrame. Are you mad?(lol)

I made a whileloop so it would transport all the players every 5 seconds.(The waiting delay I put in). I then created my players tablewhich is a list of all of the players in the game. After that, I created a for i, v in pairs loop which basically runs the amount of #players their are in the list. I then waited until the characterwas there. After that, I made sure a few elements of a character were there and not nil by creating variables and using an if statement. Afterwards, I used the MoveTo() event to position the players character.

You can read more about for pairs loops here. Read on tables here

The script should look like this.(You can take the while loop out if needed, but I expected it would be for a gameloop)

while(true) do
local players = game.Players:GetPlayers()
wait(5)
for i, v in pairs(players) do
    if(v) then
        repeat wait() until v.Character
        local head = v.Character:FindFirstChild("Head")
        local humanoid = v.Character:FindFirstChild("Humanoid")
        local torso = v.Character:FindFirstChild("Torso")
        if(head and humanoid and torso) then
            v.Character:MoveTo(game.Workspace.NPC.Harold.Head.Position)
            end
        end
    end
end

If you really love CFrame, you can use this code for the CFrame instead of the MoveTo() event.

while(true) do
local players = game.Players:GetPlayers()
wait(5)
for i, v in pairs(players) do
    if(v) then
        repeat wait() until v.Character
        local head = v.Character:FindFirstChild("Head")
        local humanoid = v.Character:FindFirstChild("Humanoid")
        local torso = v.Character:FindFirstChild("Torso")
        if(head and humanoid and torso) then
            torso.CFrame = game.Workspace.NPC.Harold.Head.CFrame
            end
        end
    end
end

0
Try to stick to the specific question and its problem instead of writing a bunch of your own code - it often only confuses the asker. Perci1 4988 — 10y

Answer this question