Is there a more performant and efficient way for the party script?

Basically, i made a loop to actualise player list constantly ensuring the players have the stats etc, however i feel like the button fire multiple events at the same time and in performance i get a huge memory leak. Anyone have solutions?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Indentify remotes
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents",3)
local PartyRemotes = RemoteEvents:WaitForChild("Party",3)

local CallPartyInvitation = PartyRemotes.CallPartyInvitation
local StartParty = PartyRemotes.StartParty
local SendPartyInvitation = PartyRemotes.SendPartyInvitation

local scrollingframe = script.Parent.ScrollingHolder.ScrollingFrame

function ColorButton(template,player)

	if not template then
		print("no template")
		return
	end

	if player and player:FindFirstChild("PlayerStats") and player.PlayerStats:FindFirstChild("PartyStats") then

		if player.PlayerStats.PartyStats.Party.Value == Players.LocalPlayer.UserId then
			template.Invite.Text.Text = "Kick"
			template.Invite.BackgroundColor3 = Color3.new(1, 0, 0)

			template.Invite.TextButton.MouseButton1Click:Connect(function()
				CallPartyInvitation:FireServer(player,"Kick")
			end)

			return
		end

		if player.UserId == Players.LocalPlayer.UserId then
			template.Invite.Text.Text = "You"
			template.Invite.BackgroundColor3 = Color3.new(0.666667, 0.666667, 0.666667)

			return
		end

		if player.PlayerStats.PartyStats.PartyInviteId.Value == Players.LocalPlayer.UserId then
			template.Invite.Text.Text = "Invited"
			template.Invite.BackgroundColor3 = Color3.new(0.666667, 0.666667, 0.666667)

			return
		end

		if player.PlayerStats.PartyStats.PartyInviteId.Value == 0 and player.PlayerStats.PartyStats.Party.Value == 0 and player.UserId ~= Players.LocalPlayer.UserId then
			template.Invite.Text.Text = "Invite"
			template.Invite.BackgroundColor3 = Color3.new(0, 0.666667, 0)

			template.Invite.TextButton.MouseButton1Click:Connect(function()
				CallPartyInvitation:FireServer(player,"Invite")
			end)

			return
		end
	end
end

script.Parent.Start.TextButton.MouseButton1Click:Connect(function()
	StartParty:FireServer()
end)

local PlayersInYourParty = {}

while wait(1) do
	PlayersInYourParty = {}
	
	for _,frame in scrollingframe:GetChildren() do
		if frame:IsA("Frame") and frame.Name ~= "Template" and not Players:FindFirstChild(frame.Name) then
			frame:Destroy()
		end
	end

	for i, player in Players:GetPlayers() do
		if not scrollingframe:FindFirstChild(player.Name) then
			print("Added frame")
			
			local template = scrollingframe.Template:Clone()

			template.Parent = scrollingframe
			template.Visible = true
			template.Name = player.Name
			template.TextLabel.Text = player.Name

			local success, errorm = pcall(function()
				template.FrameForImage.PlayerImage.Image = Players:GetUserThumbnailAsync(player.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size60x60)
			end)

			if not success then
				print(errorm)
			end
			
			ColorButton(template,player)
		end

		-- Color the button depending if user is free
		for _,frame in scrollingframe:GetChildren() do
			if frame.Name == player.Name then
				template = frame
				
				ColorButton(template,player)
			end
		end
		
		if player.PlayerStats.PartyStats.Party.Value == Players.LocalPlayer.UserId then
			table.insert(PlayersInYourParty,player)
		end
	end
	
	script.Parent.ActivePlayers.Text = #PlayersInYourParty.."/50"
	if #PlayersInYourParty > 0 then
		script.Parent.Start.BackgroundColor3 = Color3.new(0.333333, 1, 0)
	else
		script.Parent.Start.BackgroundColor3 = Color3.new(0.298039, 0.298039, 0.298039)
	end
end

The script has to handle multiple connection and frames. Which is why it was complicated for me to make a working formula of the system.

Is it not possible in this script to only update stats when a player joins? I fail to see why you cannot use game.Players.PlayerAdded:Connect().

i posted another post about the issue and fixed, however i used PlayerAdded but im not sure how long it will last until i get errors.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.