Hello, I’m making a game and don’t understand how to script a teleport to spawn after killing a mob, for example, I kill a mob, and I have to teleport to spawn, how to do this?
How to make tp to spawn after kill a mob?
1 answer
Well first you are going to want to spawn or build a mob. You can either find one in the toolbox or make your own. Now also you are going to want to get a sword or anything that can damage the mob. Now place the sword in the starter pack. Now this is the best way to do this because you are probably going to have multiple people playing in the same server so, create a part and name it "TPPart". Now create a script on the part and type in
place = CFrame.new(0,0,0) -- Where you want the player to go on touched
script.Parent.Touched:connect(function(p)--Creating the function
local humanoid = p.Parent:findFirstChild("Humanoid")--Trying to find the humanoid
if (humanoid ~= nil) then -- Checking if what touched it IS a player.
humanoid.Torso.CFrame = place -- Moves the torso to what is specified in the place variable
script.Parent:Destroy() --Destroys the part after the player is teleported
end
end)--Ending the function, if you see on the function line above there is an unclosed parenthesis that I am closing here.
Make sure to change the place = to the position of where you want the player to be teleported.
Now put the part in replicated storage (you'll see why later). Now create a script inside of the mob that you chose. Name the script anything. Now type in this
script.Parent.Humanoid.Changed:connect(function()
if script.Parent.Humanoid.Health == 0 then --detects when died
script.Disabled = true
print("Done")
local tpClone = game.ReplicatedStorage.TPPart:Clone() --refers to part
tpClone.Parent = game.Workspace --tps the part into the workspace where it was when you spawned it in
end
end)
Then there you go you are done!
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
you can just use a died event
It only print "Done" but not tp to part