How would I detect if an object already has function connected?
I made a custom leaderboard but when a player changes i connected a changed property to it but it keeps connecting a function to it so it is lagging a lot pls help
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Scroller = script.Parent:WaitForChild("Scroller")
local PlayerTemplate = script["p_"]:Clone()
local TeamTemplate = script["t_"]:Clone()
script["p_"]:Destroy()
script["t_"]:Destroy()
function LoadLeaderboard()
for i,v in pairs(Scroller:GetChildren()) do
if v:IsA("GuiBase") then
v:Destroy()
end
end
for i,v in pairs(game.Players:GetPlayers()) do
v:GetPropertyChangedSignal("Team"):Connect(function()
LoadLeaderboard()
end)
local plrgui = PlayerTemplate:Clone()
plrgui.Username.Text = v.DisplayName
plrgui.Name = plrgui.Name .. v.UserId
plrgui.Parent = Scroller
for a,b in pairs(Teams:GetChildren()) do
if b:IsA("Team") then
local teamgui = TeamTemplate:Clone()
teamgui.TeamName.Text = b.Name
teamgui.TeamName.BackgroundColor = b.TeamColor
teamgui.Name = teamgui.Name .. a
if v.Team == b then
plrgui.Parent = teamgui
elseif not v.Team then
if not Scroller:FindFirstChild(TeamTemplate.Name .. "n") then
local neutralteam = TeamTemplate:Clone()
neutralteam.TeamName.Text = "Neutral"
neutralteam.TeamName.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
neutralteam.Name = neutralteam.Name .. "n"
plrgui.Parent = neutralteam
neutralteam.Parent = Scroller
end
end
teamgui.Parent = Scroller
end
end
end
end
LoadLeaderboard()
Players.PlayerAdded:Connect(LoadLeaderboard)
Players.PlayerRemoving:Connect(LoadLeaderboard)
Teams.ChildAdded:Connect(LoadLeaderboard)
Teams.ChildRemoved:Connect(LoadLeaderboard)
Here I edited something where you can disconnect a function.
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Scroller = script.Parent:WaitForChild("Scroller")
local PlayerTemplate = script["p_"]:Clone()
local TeamTemplate = script["t_"]:Clone()
script["p_"]:Destroy()
script["t_"]:Destroy()
function LoadLeaderboard()
for i,v in pairs(Scroller:GetChildren()) do
if v:IsA("GuiBase") then
v:Destroy()
end
end
for i,v in pairs(game.Players:GetPlayers()) do
--RIGHT HERE!!
--I CHANGED SOMETHING
local propertyChangedSignal = v:GetPropertyChangedSignal("Team"):Connect(function()
LoadLeaderboard()
end)
propertyChangedSignal:Disconnect()
local plrgui = PlayerTemplate:Clone()
plrgui.Username.Text = v.DisplayName
plrgui.Name = plrgui.Name .. v.UserId
plrgui.Parent = Scroller
for a,b in pairs(Teams:GetChildren()) do
if b:IsA("Team") then
local teamgui = TeamTemplate:Clone()
teamgui.TeamName.Text = b.Name
teamgui.TeamName.BackgroundColor = b.TeamColor
teamgui.Name = teamgui.Name .. a
if v.Team == b then
plrgui.Parent = teamgui
elseif not v.Team then
if not Scroller:FindFirstChild(TeamTemplate.Name .. "n") then
local neutralteam = TeamTemplate:Clone()
neutralteam.TeamName.Text = "Neutral"
neutralteam.TeamName.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
neutralteam.Name = neutralteam.Name .. "n"
plrgui.Parent = neutralteam
neutralteam.Parent = Scroller
end
end
teamgui.Parent = Scroller
end
end
end
end
LoadLeaderboard()
Players.PlayerAdded:Connect(LoadLeaderboard)
Players.PlayerRemoving:Connect(LoadLeaderboard)
Teams.ChildAdded:Connect(LoadLeaderboard)
Teams.ChildRemoved:Connect(LoadLeaderboard)
When you disconnect a connection, that detection of GetPropertyChangedSignal() will no longer fire that signal again.
Try to have some kind of condition to check if it is at the point where the function is no longer needed, and disconnect that function that shouldn’t last further.
Like, have a thing where you got to a point where the function will be useless and disconnect it.
Also, please elaborate on what is the purpose of reloading the Leaderboard, if you are trying to change the value of the team on some kind of TextLabel you could just get that frame of that player and change the value instead of having to reload the whole thing.
Im reloading the leaderboard because i dont want to have to add a variable for each guiobject to get destroyed if a player leaves and i have teams inside it so it wouldnt work if i didnt have it reload the whole thing
And also this solution works exactly how i wanted my leaderboard to, thx
heres my script now:
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Scroller = script.Parent:WaitForChild("Scroller")
local PlayerTemplate = script["p_"]:Clone()
local TeamTemplate = script["t_"]:Clone()
local FunctionsConnected = {}
script["p_"]:Destroy()
script["t_"]:Destroy()
function LoadLeaderboard()
for i,v in pairs(Scroller:GetChildren()) do
if v:IsA("GuiBase") then
v:Destroy()
end
end
for i,v in pairs(FunctionsConnected) do
v:Disconnect()
FunctionsConnected[i] = nil
end
for i,v in pairs(game.Players:GetPlayers()) do
local propertyChangedSignal = v:GetPropertyChangedSignal("Team"):Connect(function()
LoadLeaderboard()
end)
table.insert(FunctionsConnected, propertyChangedSignal)
local plrgui = PlayerTemplate:Clone()
plrgui.Username.Text = v.DisplayName
plrgui.Name = plrgui.Name .. v.UserId
plrgui.Parent = Scroller
for a,b in pairs(Teams:GetChildren()) do
if b:IsA("Team") then
local teamgui = TeamTemplate:Clone()
teamgui.TeamName.Text = b.Name
teamgui.TeamName.BackgroundColor = b.TeamColor
teamgui.Name = teamgui.Name .. a
if v.Team == b then
plrgui.Parent = teamgui
elseif not v.Team then
if not Scroller:FindFirstChild(TeamTemplate.Name .. "n") then
local neutralteam = TeamTemplate:Clone()
neutralteam.TeamName.Text = "Neutral"
neutralteam.TeamName.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
neutralteam.Name = neutralteam.Name .. "n"
plrgui.Parent = neutralteam
neutralteam.Parent = Scroller
end
end
teamgui.Parent = Scroller
end
end
end
end
LoadLeaderboard()
Players.PlayerAdded:Connect(LoadLeaderboard)
Players.PlayerRemoving:Connect(LoadLeaderboard)
Teams.ChildAdded:Connect(LoadLeaderboard)
Teams.ChildRemoved:Connect(LoadLeaderboard)