I’m new to using Trove. I came across it when I learned that I have to disconnect all functions whenever a player leaves, so I decided to use it exactly for that.
Is this a good way of doing so, or is there a better way?
local RStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RService = game:GetService("RunService")
local Trove = require(RStorage.Trove)
local PlayersTroves = {}
-- whenever a player joins, create a new trove for them and put functions inside of it.
Players.PlayerAdded:Connect(function(Player)
PlayersTroves[Player.Name] = Trove.new()
-- example of a function
PlayersTroves[Player.Name]:Connect(RService.Stepped, function()
-- script of whatever i want to happen
end)
end)
-- destroy
Players.PlayerRemoving:Connect(function(Player)
PlayersTroves[Player.Name]:Destroy()
PlayersTroves[Player.Name] = nil
end)
Yes! I’d say that is a good way of using Troves to handle connections when they are no longer needed. Just make sure to consider the case in which the player joins before the script is ran
local RStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RService = game:GetService("RunService")
local Trove = require(RStorage.Trove)
local PlayersTroves = {}
-- whenever a player joins, create a new trove for them and put functions inside of it.
local function onPlayerAdded(Player)
PlayersTroves[Player.Name] = Trove.new()
-- example of a function
PlayersTroves[Player.Name]:Connect(RService.Stepped, function()
-- script of whatever i want to happen
end)
end
local function onPlayerLeave(Player)
PlayersTroves[Player.Name]:Destroy()
PlayersTroves[Player.Name] = nil
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerLeave)
for _, player in Players:GetPlayers() do
task.defer(onPlayerAdded, player)
end