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

Player isn't being teleported to a random position on a part?

Asked by
ExcelUp 24
5 years ago

I have a script which, when a player respawns and the boolean "EventEnabled" is true, the player should teleport to a random position on a part called "Pad", inside a model called "Box", found in the Workspace.

When the script runs, it does nothing, when it should. I've tried adding print statements to see if the script is accessing the player's character correctly, such as print(Character.Name) or print(Character.HumanoidRootPart.Position), and it is accessing the player's character correctly. It just simply won't move the player, when it should.

This is the code I have:

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        if EventEnabled then
            local Pad = game:GetService("Workspace"):FindFirstChild("Box").Pad
            local MinX = Pad.Position.X - Pad.Size.X / 2
            local MaxX = Pad.Position.X + Pad.Size.X / 2
            local MinZ = Pad.Position.Z - Pad.Size.Z / 2
            local MaxZ = Pad.Position.Z + Pad.Size.Z / 2
            print(Character.Name)
            print(Character.HumanoidRootPart.Position)
            Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(math.random(MinX, MaxX), Pad.Position.Y + 3, math.random(MinZ, MaxZ)))
        end
    end)
end
0
CFrame.new arguments (X, Y, Z) takes numbers, not Vector3's. You only have one argument (X) which is a Vector3. Character.HumanoidRootPart.CFrame = CFrame.new( math.random(MinX, MaxX), Pad.Position.Y + 3, math.random(MinZ, MaxZ) ) pidgey 548 — 5y
0
Still doesn't work. ExcelUp 24 — 5y
0
You have a different problem then, try using a breakpoint at the start of the PlayerAdded event and see where it takes you. Be descriptive pidgey 548 — 5y
0
Also, line 14 is missing a closing parenthesis to close Connect which would be a compile-time error. pidgey 548 — 5y
0
I figured out the issue. Even though the character was added, it wasn't fully loaded, so the code was running before the HumanoidRootPart was even loaded in. I added "repeat wait() until Player.Character", to combat this, and it works now. Thank you for your help, regardless. ExcelUp 24 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

OK i'm making a shooting game and it teleports the player/players as well so here is how to do it

here is the script

this is just a teleport script

local LowerTorso = game.Players.LocalPlayer.Character:WaitForChild("Torso") --- if R15 change "Torso" to "LowerTorso"

game:GetService("Players").PlayerAdded:Connect(function(Player)
  Player.CharacterAdded:Connect(function(Character)
  if EventEnabled then


local num = math.random(1,6) --- change the 6 to how much pads there are DON'T THOUGH 1
LowerTorso.CFrame = CFrame.new(game.Workspace.Box["Pad"..num].Position) --- change the word "Pad" to the spawn/teleport thing's name

Ad

Answer this question