Teleport players after finishing a boss fight

  1. What do you want to achieve? So basically after a boss fight, I want all the remaining player to be teleported to the end zone.

  2. What is the issue? It won’t teleport the players.

  3. What solutions have you tried so far? Trying to figure it out, and using other teleport scripts for reference.

local players = game.Players.LocalPlayer

if script.Parent.NPC.Health == 0 then
local Pos = game.Workspace.TouchedPart --Part
	players:moveTo(Pos.Position)
end

If I sound really stupid, it is because I am a beginning scripter! So if you catch anything I do or say wrong please correct me!

1 Like

You can’t teleport players through the client. You will need to do that on the server.

for _, player in pairs(game.Players:GetChildren()) do
        player:MoveTo(Pos.Position)
end

Another thing, it looks like your code isn’t hooked up to an event or anything, so if script.Parent.NPC.Health == 0 then will only run once at the start, and it will not pass. When the NPC takes damage, you will need to check it’s health and if it’s at 0. Then you can teleport the players.

So like this?

 '''
  local players = game.Players.LocalPlayer

  if script.Parent.NPC.Health == 0 then

local Pos = game.Workspace.TouchedPart --Part
for _, player in pairs(game.Players:GetChildren()) do
player:MoveTo(Pos.Position)
end
/ ‘’’

Again, this won’t work in a local script. You can teleport the players on a Humanoid.Died function.

Put this script inside of a normal Script

local Pos = game.Workspace.TouchedPart

script.Parent.NPC.Died:Connect(function() -- NPC is a humanoid object, correct?
        -- this function will run once the NPC is dead
        for _, player in pairs(game.Players:GetChildren()) do
                player:MoveTo(Pos.Position)
        end
end)

You cannot call player:MoveTo(pos) becuase that method is from the Model class. There is also one for the Humanoid class but that is different and will have the humanoid attempt to walk to the position instead of teleporting.

You can change the character’s HumanoidRootPart CFrame or change the Position property. Another way you can teleport the character is to use player.Character:MoveTo(pos) basically the same thing as setting the Model’s primary part to the argument position.

This way uses the CFrame method and I cleaned up your code so it’s more readible.

local Players = game:GetService("Players")

local teleportLocationPart = game.Workspace.TouchedPart
local npc = script.Parent
local npcHumanoid = npc:FindFirstChild("Humanoid")

npcHumanoid.Died:Connect(function()
    for _, player in pairs(Players:GetChildren()) do
        local character = player.Character
        local rootPart = nil
    
        if not character then
            return
        else
            rootPart = character:FindFirstChild("HumanoidRootPart")
            if rootPart then
                rootPart.CFrame = CFrame.new(teleportLocationPart.Position)
            end
        end
    end
end)
1 Like

Thanks for attempting, but for some odd reason, It doesn’t teleport me to the designated location.

Where is the script parented to? It would help if you provide screenshots of your explorer’s hierarchy where your npc and the script is located. Is it a local script? Are there any errors? If not then your setup is wrong.

The script is parented to workspace, and there are no errors in the output and script analysis. If need me to take a video of it, tell me.

By looking at the script I’m pretty sure the script should be inside of the NPC.

In specific: local npc = script.Parent

2 Likes

FYI: The “NPC” is the humanoid of the boss.

The script shouldn’t be inside of the Humanoid, it should be inside of the model. The script looks for the Humanoid inside the model.

This line of code right here: local npcHumanoid = npc:FindFirstChild(“Humanoid”)

What is that line of code for? It is already included in the script provided by @ImpactHills

Exactly, I was just referring to that line of code. I was explaining that this line of code automatically searches for the Humanoid inside of the boss model, so you don’t have to put the script inside of the Humanoid.

-- server script
local Players = game:GetService("Players")
for _,v in pairs(Players:GetChildren()) do
     local Character = game.Workspace[v.Name]
     Character:MoveTo(Vector3.new(0,0,0)) -- Location
     -- or use
    Character:MoveTo(ThePart.CFrame.p) -- To teleport to a part!
end

In a server script:

--reference the thinghere in an if statement 

for _, v in pairs(game.Players:GetPlayers()) do
v:LoadCharacter()
end

LoadCharacter will basically reset the character back to spawn.

If you want to teleport to a specific location, use HumanoidRootPart or Torso depending if your game uses r6 or r15.

for _, v in pairs(game.Players:GetPlayers()) do
v.Character.HumanoidRootPart.Position = Vector3.new() -- or reference the torso.
end

Thanks for helping me, but the script doesn’t function, and there is no error in output and script analysis.