Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local GrowPlace = {}
- local activeHouses = {}
- --------------------Services--------------------|
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- --------------------Events--------------------|
- local Events = ReplicatedStorage:WaitForChild("Events")
- --RemoteFunctions
- local RemoteFunctions = Events:WaitForChild("RemoteFunctions")
- --------------------Houses--------------------|
- local growPlace = ReplicatedStorage:WaitForChild("GrowPlace")
- local Houses = growPlace:WaitForChild("Houses")
- --Houses
- local Abandonedhouse = Houses:WaitForChild("Abandoned house")
- --House positions
- local AbandonedhousePositions = {
- [1] = Vector3.new(639, -31, -375),
- [2] = Vector3.new(677, -31, -375),
- [3] = Vector3.new(677, -31, -312),
- [4] = Vector3.new(639, -31, -312),
- [5] = Vector3.new(677, -31, -441),
- [6] = Vector3.new(639, -31, -441),
- [7] = Vector3.new(639, -49, -375),
- [8] = Vector3.new(677, -49, -375),
- [9] = Vector3.new(677, -49, -312),
- [10] = Vector3.new(639, -49, -312),
- [11] = Vector3.new(677, -49, -441),
- [12] = Vector3.new(639, -49, -441),
- [13] = Vector3.new(677, -66, -375),
- [14] = Vector3.new(677, -66, -312),
- [15] = Vector3.new(639, -66, -312),
- [16] = Vector3.new(677, -66, -441),
- [17] = Vector3.new(639, -66, -441),
- [18] = Vector3.new(639, -84, -375),
- [19] = Vector3.new(677, -84, -375),
- [20] = Vector3.new(677, -84, -312),
- [21] = Vector3.new(677, -84, -441),
- [22] = Vector3.new(639, -84, -441),
- [23] = Vector3.new(639, -84, -312),
- [24] = Vector3.new(639, -66, -375),
- [25] = Vector3.new(639, -105, -441),
- [26] = Vector3.new(639, -105, -375),
- [27] = Vector3.new(639, -105, -312),
- [28] = Vector3.new(677, -105, -441),
- [29] = Vector3.new(677, -105, -375),
- [30] = Vector3.new(677, -105, -312)
- }
- local function LoadGrowPlaceItems(player, playerData, house)
- local plot = house:FindFirstChild("Plot")
- for i, Pos in pairs(playerData[player].GrowPlace) do
- -- Extract base name by removing trailing numbers from the end of 'i'
- local baseName = i:match("^(%a+)")
- -- Find the item in ReplicatedStorage using the base name
- local item = ReplicatedStorage.GrowPlace.GrowItems:FindFirstChild(baseName)
- if item then
- local Cloneditem = item:Clone()
- Cloneditem.Parent = plot.GrowItemHolder
- Cloneditem.Name = i
- -- Extract position and orientation from the saved data
- local position = Vector3.new(unpack(Pos.cframe.position))
- local orientation = CFrame.Angles(
- math.rad(Pos.cframe.orientation[1]),
- math.rad(Pos.cframe.orientation[2]),
- math.rad(Pos.cframe.orientation[3])
- )
- -- Calculate the world CFrame and set it for the cloned item
- local relativeCFrame = CFrame.new(position) * orientation
- local worldCFrame = plot.CFrame * relativeCFrame
- if Cloneditem.PrimaryPart then
- Cloneditem:PivotTo(worldCFrame)
- else
- warn("PrimaryPart not set for item:", Cloneditem.Name)
- end
- else
- if baseName ~= "GPLevel" and baseName ~= "Lock" then
- warn("Item not found:", baseName)
- end
- end
- end
- end
- function GrowPlace.SpawnInPlot(player, playerdata)
- local house = playerdata[player].GrowPlace.GPLevel
- local houseName = house .. player.Name
- -- Remove existing house if any, before spawning in a new one
- if activeHouses[player] then
- activeHouses[player]:Destroy()
- activeHouses[player] = nil
- end
- -- Find an avaible position for the house
- for i, pos in ipairs(AbandonedhousePositions) do
- if not activeHouses[pos] then
- local clonedHouse = Houses:WaitForChild(house):Clone()
- clonedHouse.Name = houseName
- clonedHouse.Parent = workspace:WaitForChild("Houses")[playerdata[player].GrowPlace.GPLevel]
- local houseCFrame = CFrame.new(pos) * CFrame.Angles(0,math.rad(90),0)
- clonedHouse:SetPrimaryPartCFrame(houseCFrame)
- activeHouses[player] = clonedHouse
- activeHouses[pos] = true
- break
- end
- end
- LoadGrowPlaceItems(player, playerdata, activeHouses[player])
- end
- -- Clean up the player's house when they leave
- function GrowPlace.RemoveHouseOnLeave(player)
- -- Check if a house exists for the player in activeHouses
- local playerHouse = activeHouses[player]
- if playerHouse then
- -- Check if the house has a PrimaryPart to access Position safely
- if playerHouse.PrimaryPart then
- local pos = playerHouse.PrimaryPart.Position
- -- Mark the position as available
- activeHouses[pos] = nil
- else
- warn("PrimaryPart not set for player house:", playerHouse.Name)
- end
- -- Destroy the house and remove player entry
- playerHouse:Destroy()
- activeHouses[player] = nil
- else
- warn("No house found to remove for player:", player.Name)
- end
- end
- return GrowPlace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement