Creating a text button (similar to on a player-list) that sends a friend request to the player

Hi, I would to know how to make a text button with the other player name and all I have to do is click it and that will send a friend request(kinda like custom player list). I been reading the wiki but still confuse please HELP!

3 Likes

Firstly, you’ll need a Script in ServerScriptService, a LocalScript inside the ScreenGui or wherever it is and a RemoteEvent inside ReplicatedStorage.

Inside the Script you’ll need to connect on PlayerAdded and PlayerRemoving, when you do FireAllClients the RemoteEvent:

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

players.PlayerAdded:Connect(function()
    rs.RemoteEvent:FireAllClients()
end)

players.PlayerRemoving:Connect(function()
    rs.RemoteEvent:FireAllClients()
end)

Now inside the LocalScript you’ll need to setup the List, for each button make it so on MouseButton1Click it grabs the name of the TextButton and prompts a friend request using setcore:

local players = game:GetService("Players")

function setup()
    local plrs = players:GetPlayers()
    --Make sure to delete any old textbuttons
    for _, player in pairs(plrs) do
        if not player.UserId == players.LocalPlayer.UserId then --You don't wanna set it for yourself
            local textbutton = Instance.new("TextButton")
            textbutton.Parent = script.Parent --Where you need to put this
            textbutton.Name = player.Name --make sure to do this
            --Make sure to position it too!

            textbutton.MouseButton1Click:Connect(function()
                local name = textbutton.Name
                local clickedPlayer = players:FindFirstChild(name) --Use FFC to ensure no errors
                if clickedPlayer then --ensure they exist
                    game:GetService("StarterGui"):SetCore("PromptSendFriendRequest", clickedPlayer)
                end
            end)
        end
    end
end

setup() --call it once for when you join
game:GetService("ReplicatedStorage").RemoteEvent.OnClientEvent:Connect(setup) --setup when a player leaves/joins
9 Likes

OMg Where have you been all my life

1 Like

Does this not work anymore? I’ve tried to make a similar system where it lists all the players (using this code) but it doesn’t create a TextButton. Even if I just make a straight up copy of everything here it still doesn’t work, there are no errors it just doesn’t create a textbutton for players.

yes it still work is using remote event.

That’s exactly what I’m doing as per his instructions but nothing is happening, no error message or anything, I’m still stumped about this.