Advertisement
Inqxii

Untitled

Mar 25th, 2023
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. -- [[ Modules / Instances ]] --
  2. local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
  3. local library = require(game:GetService("ServerScriptService").Server.Modules:WaitForChild("Library"))
  4. local players = game:GetService("Players")
  5. local islandsFolder = library.islands.islandsFolder
  6. local globalRadius = library.islands.globalRadius
  7. local baseIslandsFolder = library.islands.baseIslandsFolder
  8.  
  9. local services = {}
  10.  
  11. -- [[ Private Function / Variables]] --
  12. local function getXAndZPositions(r, angle) -- Gets the X and Z positions for new islands on a circle
  13.     local x = math.sin(math.rad(angle)) * r
  14.     local z = math.cos(math.rad(angle)) * r
  15.  
  16.     return x, z
  17. end
  18.  
  19. local unloadedIslands = {} -- Will store all islands that were or were not yet loaded into the game or taken a spot
  20. local islands = {} -- Will store all islands inside this table
  21.  
  22. -- [[ Service ]] --
  23. local islandService = Knit.CreateService {
  24.     Name = "IslandService",
  25.     Client = {}
  26. }
  27. islandService.__index = islandService
  28.  
  29. function islandService:new(player: Player , spot: number)
  30.     local newIsland = {}
  31.     setmetatable(newIsland, islandService)
  32.     newIsland.Spot = spot or nil
  33.     newIsland.Player = player or nil
  34.     newIsland.Island = nil
  35.  
  36.     return newIsland
  37. end
  38.  
  39. function islandService:getIsland(player: Player)
  40.     for i, v in pairs(islands) do
  41.         if v.Player == player then
  42.             return v
  43.         end
  44.     end
  45. end
  46.  
  47. function islandService:init(player: Player, setSpawn: boolean) -- Creates a new Island inside the IslandsFolder and assigns a player to it
  48.     for i = 1, 8, 1 do
  49.         if islands[i] then continue end
  50.         if not islands[i] then
  51.             local x, z = getXAndZPositions(globalRadius, 45*(i-1))
  52.             local newIsland = baseIslandsFolder:WaitForChild("BasicIsland"):Clone()
  53.             self.Island = newIsland
  54.             newIsland:WaitForChild("IslandInfo").Player.Value = player
  55.             newIsland.Name = player.Name
  56.             self.Player = player
  57.             self.Spot = i
  58.             newIsland.Parent = islandsFolder.IslandModels
  59.             print("Initialized "..player.Name.."'s island")
  60.  
  61.             local lastReturn = 0
  62.  
  63.             newIsland:PivotTo(CFrame.new(x, 20, z) * CFrame.Angles(0, math.rad(45*(i-1) - 90), 0))
  64.  
  65.             if setSpawn == true then
  66.                 player.RespawnLocation = self.Island:WaitForChild("Island").Spawn.Spawn
  67.             end
  68.             islands[i] = self
  69.             return newIsland
  70.         end
  71.     end
  72. end
  73.  
  74. function islandService:destroy() -- Destroys an island and its game instance, typically called on PlayerLeaving
  75.     if self.Island ~= nil then
  76.         self.Island:Destroy()
  77.     end
  78.     if islands[self.Spot] then
  79.        table.remove(islands, self.Spot)
  80.     end
  81.     self = nil
  82. end
  83.  
  84.  
  85. function islandService.Client:getIsland(player)
  86.     return islandService:getIsland(player)
  87. end
  88.  
  89. function islandService:KnitStart() -- Accesses services
  90.    
  91. end
  92.  
  93. function islandService:KnitInit() -- Initializes this service
  94.     players.PlayerAdded:Connect(function(plr)
  95.         local newIsland = islandService:new(plr)
  96.         newIsland:init(plr, true)
  97.     end)
  98. end
  99.  
  100. return islandService
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement