Advertisement
BADABATS

House Spawn

May 20th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. local GrowPlace = {}
  2. local activeHouses = {}
  3.  
  4. --------------------Services--------------------|
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6.  
  7. --------------------Events--------------------|
  8. local Events = ReplicatedStorage:WaitForChild("Events")
  9.  
  10. --RemoteFunctions
  11. local RemoteFunctions = Events:WaitForChild("RemoteFunctions")
  12.  
  13. --------------------Houses--------------------|
  14. local growPlace = ReplicatedStorage:WaitForChild("GrowPlace")
  15.  
  16. local Houses = growPlace:WaitForChild("Houses")
  17.  
  18. --Houses
  19. local Abandonedhouse = Houses:WaitForChild("Abandoned house")
  20.  
  21. --House positions
  22. local AbandonedhousePositions = {
  23. [1] = Vector3.new(639, -31, -375),
  24. [2] = Vector3.new(677, -31, -375),
  25. [3] = Vector3.new(677, -31, -312),
  26. [4] = Vector3.new(639, -31, -312),
  27. [5] = Vector3.new(677, -31, -441),
  28. [6] = Vector3.new(639, -31, -441),
  29. [7] = Vector3.new(639, -49, -375),
  30. [8] = Vector3.new(677, -49, -375),
  31. [9] = Vector3.new(677, -49, -312),
  32. [10] = Vector3.new(639, -49, -312),
  33. [11] = Vector3.new(677, -49, -441),
  34. [12] = Vector3.new(639, -49, -441),
  35. [13] = Vector3.new(677, -66, -375),
  36. [14] = Vector3.new(677, -66, -312),
  37. [15] = Vector3.new(639, -66, -312),
  38. [16] = Vector3.new(677, -66, -441),
  39. [17] = Vector3.new(639, -66, -441),
  40. [18] = Vector3.new(639, -84, -375),
  41. [19] = Vector3.new(677, -84, -375),
  42. [20] = Vector3.new(677, -84, -312),
  43. [21] = Vector3.new(677, -84, -441),
  44. [22] = Vector3.new(639, -84, -441),
  45. [23] = Vector3.new(639, -84, -312),
  46. [24] = Vector3.new(639, -66, -375),
  47. [25] = Vector3.new(639, -105, -441),
  48. [26] = Vector3.new(639, -105, -375),
  49. [27] = Vector3.new(639, -105, -312),
  50. [28] = Vector3.new(677, -105, -441),
  51. [29] = Vector3.new(677, -105, -375),
  52. [30] = Vector3.new(677, -105, -312)
  53. }
  54.  
  55. local function LoadGrowPlaceItems(player, playerData, house)
  56. local plot = house:FindFirstChild("Plot")
  57.  
  58.  
  59. for i, Pos in pairs(playerData[player].GrowPlace) do
  60. -- Extract base name by removing trailing numbers from the end of 'i'
  61. local baseName = i:match("^(%a+)")
  62.  
  63. -- Find the item in ReplicatedStorage using the base name
  64. local item = ReplicatedStorage.GrowPlace.GrowItems:FindFirstChild(baseName)
  65.  
  66. if item then
  67. local Cloneditem = item:Clone()
  68. Cloneditem.Parent = plot.GrowItemHolder
  69. Cloneditem.Name = i
  70.  
  71. -- Extract position and orientation from the saved data
  72. local position = Vector3.new(unpack(Pos.cframe.position))
  73. local orientation = CFrame.Angles(
  74. math.rad(Pos.cframe.orientation[1]),
  75. math.rad(Pos.cframe.orientation[2]),
  76. math.rad(Pos.cframe.orientation[3])
  77. )
  78.  
  79. -- Calculate the world CFrame and set it for the cloned item
  80. local relativeCFrame = CFrame.new(position) * orientation
  81. local worldCFrame = plot.CFrame * relativeCFrame
  82.  
  83. if Cloneditem.PrimaryPart then
  84. Cloneditem:PivotTo(worldCFrame)
  85. else
  86. warn("PrimaryPart not set for item:", Cloneditem.Name)
  87. end
  88. else
  89. if baseName ~= "GPLevel" and baseName ~= "Lock" then
  90. warn("Item not found:", baseName)
  91. end
  92. end
  93. end
  94. end
  95.  
  96. function GrowPlace.SpawnInPlot(player, playerdata)
  97. local house = playerdata[player].GrowPlace.GPLevel
  98. local houseName = house .. player.Name
  99. -- Remove existing house if any, before spawning in a new one
  100. if activeHouses[player] then
  101. activeHouses[player]:Destroy()
  102. activeHouses[player] = nil
  103. end
  104.  
  105. -- Find an avaible position for the house
  106. for i, pos in ipairs(AbandonedhousePositions) do
  107. if not activeHouses[pos] then
  108. local clonedHouse = Houses:WaitForChild(house):Clone()
  109. clonedHouse.Name = houseName
  110. clonedHouse.Parent = workspace:WaitForChild("Houses")[playerdata[player].GrowPlace.GPLevel]
  111.  
  112. local houseCFrame = CFrame.new(pos) * CFrame.Angles(0,math.rad(90),0)
  113. clonedHouse:SetPrimaryPartCFrame(houseCFrame)
  114.  
  115. activeHouses[player] = clonedHouse
  116. activeHouses[pos] = true
  117. break
  118. end
  119. end
  120.  
  121. LoadGrowPlaceItems(player, playerdata, activeHouses[player])
  122. end
  123.  
  124. -- Clean up the player's house when they leave
  125. function GrowPlace.RemoveHouseOnLeave(player)
  126. -- Check if a house exists for the player in activeHouses
  127. local playerHouse = activeHouses[player]
  128. if playerHouse then
  129. -- Check if the house has a PrimaryPart to access Position safely
  130. if playerHouse.PrimaryPart then
  131. local pos = playerHouse.PrimaryPart.Position
  132. -- Mark the position as available
  133. activeHouses[pos] = nil
  134. else
  135. warn("PrimaryPart not set for player house:", playerHouse.Name)
  136. end
  137.  
  138. -- Destroy the house and remove player entry
  139. playerHouse:Destroy()
  140. activeHouses[player] = nil
  141. else
  142. warn("No house found to remove for player:", player.Name)
  143. end
  144. end
  145.  
  146.  
  147. return GrowPlace
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement