Advertisement
Guest User

Teleport (Server Script)

a guest
Mar 3rd, 2021
3,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1.  
  2. --# Portal Teleportation
  3.  
  4. --# Define Services
  5. local Players = game:GetService('Players')
  6. local RS = game:GetService('ReplicatedStorage')
  7.  
  8. --# Define remote to tell client to fade in / fade out
  9. local fadeRemote = RS:WaitForChild('Fade')
  10.  
  11. --# Define portals that are going to be used
  12. local portalA = workspace:WaitForChild('PortalA')
  13. local portalB = workspace:WaitForChild('PortalB')
  14.  
  15. --# Hold players in table to ensure that one player cannot be teleported multiple times
  16. local playersBeingTeleported = {}
  17.  
  18. --# If a player touches a portal, this will teleport them
  19. local function teleportCharacter(char, portal)
  20. --# Ensure that the character that touched the portal is in our Players list
  21. --# (basically verifiying that an NPC/zombie doesn't teleport)
  22. if Players:FindFirstChild(char.Name) then
  23. --# Tell the Client that touched to Fade Out
  24. fadeRemote:FireClient(Players[char.Name], 'Fade Out')
  25. wait(1)
  26. --# Move the Character that touched
  27. char.PrimaryPart.CFrame = CFrame.new(portal.MovePart.Position, portal.Exit.Position)
  28. --# Tell the Client that touched to Fade In
  29. fadeRemote:FireClient(Players[char.Name], 'Fade In')
  30. wait(0.2)
  31. --# Make it appear as if the player is running out of the portal
  32. char.Humanoid:MoveTo(portal.Exit.Position)
  33.  
  34. --# Remove the player name from the table that is holding all of the players
  35. --# (this is because the teleportation effect has finished)
  36. wait(2)
  37. playersBeingTeleported[char.Name] = nil
  38. end
  39. end
  40.  
  41. --# Handling the entering of portals #--
  42.  
  43. --# If the main part of the portal is touched
  44. portalA:WaitForChild('MovePart').Touched:Connect(function(hit)
  45. --# Touched by a player (not an NPC/Zombie/etc.)
  46. if hit.Parent and hit.Parent:FindFirstChild('Humanoid') and Players:FindFirstChild(hit.Parent.Name) then
  47. --# Touched by an alive player
  48. if hit.Parent.Humanoid.Health > 0 then
  49. --# Add the players to the list of players being teleported
  50. if playersBeingTeleported[hit.Parent.Name] == nil then
  51. playersBeingTeleported[hit.Parent.Name] = 'teleporting...'
  52. --# Teleport the player
  53. teleportCharacter(hit.Parent, portalB) --# parameters are the Character to teleport, and the portal to goto
  54. end
  55. end
  56. end
  57. end)
  58.  
  59. --# Handling portalB is almost the same except for a few differences
  60.  
  61. --# Waiting for portalB to be touched (instead of portal A)
  62. portalB:WaitForChild('MovePart').Touched:Connect(function(hit)
  63. if hit.Parent and hit.Parent:FindFirstChild('Humanoid') then
  64. if hit.Parent.Humanoid.Health > 0 then
  65. if playersBeingTeleported[hit.Parent.Name] == nil then
  66. playersBeingTeleported[hit.Parent.Name] = 'teleporting...'
  67. teleportCharacter(hit.Parent, portalA) --# Going to portalA instead of portalB
  68. end
  69. end
  70. end
  71. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement