Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [[ Modules / Instances ]] --
- local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
- local library = require(game:GetService("ServerScriptService").Server.Modules:WaitForChild("Library"))
- local players = game:GetService("Players")
- local islandsFolder = library.islands.islandsFolder
- local globalRadius = library.islands.globalRadius
- local baseIslandsFolder = library.islands.baseIslandsFolder
- local services = {}
- -- [[ Private Function / Variables]] --
- local function getXAndZPositions(r, angle) -- Gets the X and Z positions for new islands on a circle
- local x = math.sin(math.rad(angle)) * r
- local z = math.cos(math.rad(angle)) * r
- return x, z
- end
- local unloadedIslands = {} -- Will store all islands that were or were not yet loaded into the game or taken a spot
- local islands = {} -- Will store all islands inside this table
- -- [[ Service ]] --
- local islandService = Knit.CreateService {
- Name = "IslandService",
- Client = {}
- }
- islandService.__index = islandService
- function islandService:new(player: Player , spot: number)
- local newIsland = {}
- setmetatable(newIsland, islandService)
- newIsland.Spot = spot or nil
- newIsland.Player = player or nil
- newIsland.Island = nil
- return newIsland
- end
- function islandService:getIsland(player: Player)
- for i, v in pairs(islands) do
- if v.Player == player then
- return v
- end
- end
- end
- function islandService:init(player: Player, setSpawn: boolean) -- Creates a new Island inside the IslandsFolder and assigns a player to it
- for i = 1, 8, 1 do
- if islands[i] then continue end
- if not islands[i] then
- local x, z = getXAndZPositions(globalRadius, 45*(i-1))
- local newIsland = baseIslandsFolder:WaitForChild("BasicIsland"):Clone()
- self.Island = newIsland
- newIsland:WaitForChild("IslandInfo").Player.Value = player
- newIsland.Name = player.Name
- self.Player = player
- self.Spot = i
- newIsland.Parent = islandsFolder.IslandModels
- print("Initialized "..player.Name.."'s island")
- local lastReturn = 0
- newIsland:PivotTo(CFrame.new(x, 20, z) * CFrame.Angles(0, math.rad(45*(i-1) - 90), 0))
- if setSpawn == true then
- player.RespawnLocation = self.Island:WaitForChild("Island").Spawn.Spawn
- end
- islands[i] = self
- return newIsland
- end
- end
- end
- function islandService:destroy() -- Destroys an island and its game instance, typically called on PlayerLeaving
- if self.Island ~= nil then
- self.Island:Destroy()
- end
- if islands[self.Spot] then
- table.remove(islands, self.Spot)
- end
- self = nil
- end
- function islandService.Client:getIsland(player)
- return islandService:getIsland(player)
- end
- function islandService:KnitStart() -- Accesses services
- end
- function islandService:KnitInit() -- Initializes this service
- players.PlayerAdded:Connect(function(plr)
- local newIsland = islandService:new(plr)
- newIsland:init(plr, true)
- end)
- end
- return islandService
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement