Advertisement
vadim2018

vip door

Sep 22nd, 2018
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. --------------------
  2. --| WaitForChild |--
  3. --------------------
  4.  
  5. -- Waits for parent.child to exist, then returns it
  6. local function WaitForChild(parent, childName)
  7. assert(parent, "ERROR: WaitForChild: parent is nil")
  8. while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
  9. return parent[childName]
  10. end
  11.  
  12. -----------------
  13. --| Variables |--
  14. -----------------
  15.  
  16. local GamePassService = game:GetService('MarketplaceService')
  17. local PlayersService = game:GetService('Players')
  18.  
  19. local VipDoor = script.Parent
  20.  
  21. local GamePassIdObject = WaitForChild(script, 'GamePassId')
  22.  
  23. local JustTouched = {}
  24.  
  25. -----------------
  26. --| Functions |--
  27. -----------------
  28.  
  29. -- Finds out which side the player is on and teleports them to the other
  30. local function TeleportToOtherSide(character, hitPart)
  31. local bottomOfDoor = VipDoor.CFrame.p - Vector3.new(0, VipDoor.Size.Y / 2, 0)
  32. local inFrontOfDoor = bottomOfDoor + VipDoor.CFrame.lookVector * 3
  33. local behindDoor = bottomOfDoor - VipDoor.CFrame.lookVector * 3
  34.  
  35. local distanceToFront = (inFrontOfDoor - hitPart.Position).magnitude
  36. local distanceToBack = (behindDoor - hitPart.Position).magnitude
  37. if distanceToFront < distanceToBack then
  38. character:MoveTo(behindDoor)
  39. else
  40. character:MoveTo(inFrontOfDoor)
  41. end
  42. end
  43.  
  44. -- When a player with the game pass touches the door, teleport them to the other side
  45. local function OnTouched(otherPart)
  46. if otherPart and otherPart.Parent and otherPart.Parent:FindFirstChild('Humanoid') then
  47. local player = PlayersService:GetPlayerFromCharacter(otherPart.Parent)
  48. if player and not JustTouched[player] then
  49. JustTouched[player] = time()
  50. if GamePassService:UserOwnsGamePassAsync(player.userId, GamePassIdObject.Value) then
  51. TeleportToOtherSide(player.Character, otherPart)
  52. end
  53. end
  54. end
  55. end
  56.  
  57. -- Removes old entries in JustTouched
  58. local function RemoveOldTouches()
  59. for player, touchTime in pairs(JustTouched) do
  60. if time() > touchTime + 0.3 then
  61. JustTouched[player] = nil
  62. end
  63. end
  64. end
  65.  
  66. --------------------
  67. --| Script Logic |--
  68. --------------------
  69.  
  70. VipDoor.Touched:connect(OnTouched)
  71.  
  72. while true do
  73. RemoveOldTouches()
  74. wait(1/30)
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement