Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Check if input (roblox username) is friends with another input (roblox username)?

So i've been trying to get an input for a roblox username, and then another input with another roblox username. Then, when I press a button, it comes up with an error. Can anyone help?

local Players = game:GetService("Players")
local Plr1 = script.Parent.Parent.Plr1.Text
local Plr2 = script.Parent.Parent.Plr2.Text
 
local function getPlayerFromName(name)
   
   for _, player in pairs(game:GetService("Players"):GetPlayers()) do
      
      if player.Name:lower() == name:lower() then
          return player
      end
   end

end

script.Parent.MouseButton1Click:Connect(function(click)
	local ID1 = getPlayerFromName(Plr1)
	local ID2 = getPlayerFromName(Plr2)
	if ID1.Value:IsFriendsWith(ID2.Value) then
		print("Yes")
	else
		print("No")
	end
end)

And then it comes up with this message: 22:15:15.245 - Players.LennyPlayzYT.PlayerGui.FriendGui.Main.Section(1).Check.LocalScript:19: attempt to index local 'ID1' (a nil value)

1 answer

Ok so:

You're making the "Plr" variables remain constant

You are taking the text of the "Plr" TextBoxes and putting it into a variable. The variable will not update if the textbox's text has changed.

You want to do:

-- outside of the "MouseButton" event

local Plr1 = script.Parent.Parent.Plr1
local Plr2 = script.Parent.Parent.Plr2

-- inside of the "MouseButton" event

local player = getPlayerName(Plr1.Text)
local player2 getPlayerName(Plr2.Text)

A player doesn't have a "Value" property

You're looking for their UserId which is a player's unique identifier. You want to do:

local isFriends = player:IsFriendsWith(player2.UserId)

if isFriends then
   -- is friends
else
   -- aren't friends
end

Also, MOUSEBUTTON1CLICK/DOWN OF A GUIBUTTON NEVER RETURNS ANYTHING

1 comment

0

But then what's the definition of 'getPlayerName'?

This discussion is preserved as it was. New answers, comments, and votes are closed.