Updating friendlist when player goes offline

I want to make it so that when a player’s friend goes offline the template of his friend gets destroyed.

local player = game.Players.LocalPlayer

local friends = player:GetFriendsOnline(20)



for i, friend in pairs(friends) do
	if friend.IsOnline == true then
	if not script.Parent:FindFirstChild(friend.UserName) then
	local frame = script.Parent.Parent.FriendTemplate:Clone()
	frame.Parent = script.Parent
	frame.PlayerName.Text = friend.UserName
	frame.Name = friend.UserName
	frame.Visible = true
	end
	
	end

end

putting it in a loop would be probably the best way

local player = game.Players.LocalPlayer

local ClearFrames = function()
	for i,v in pairs(script.Parent:GetChildren()) do 
		if (v:IsA("Frame")) then 
			v:Destroy()
		end
	end
end

task.spawn(function()
	while true and task.wait(10) do 
		ClearFrames()
		
		for i,v in pairs(player:GetFriendsOnline(20)) do 
			if (v.IsOnline == true) then 
				local Frame = script.Parent.Parent.FriendTemplate:Clone()
				Frame.PlayerName.Text = v.UserName
				Frame.Name = v.UserName 
				Frame.Visible = true 
				Frame.Parent = script.Parent
			end
		end
	end
end)


Check this (Is this possible?) post on the dev forum for more help on it.

Yes, but when the 10 seconds pass the template will be deleted even after the player’s friend is online.

and after that new ones will get created for friends that are currently online

What do you mean? That’s my post.

sry about that check it again i changed it now and I dont really know how to format the link properly still new to posting on the dev hub

Is there a better way? Because the frame will disappear for a second of the online player.

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