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.
What is the issue? It won’t teleport the players.
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!
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.
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)
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.
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