How to get what caused the then event to happen in a lot of if and ors

So I am kind of confused how to explain this, so I hope you can understand this. How would I get which textbutton was not the text I wanted it to be, so then I could turn it into that text? For example;

 local player = game.Players.LocalPlayer
 local dict = player:GetFriendsOnline(200)
 for i,friend in pairs(dict) do
	print(friend.UserName.." from this script")
if friend then
if not script.Parent.plr.Text == friend.UserName or not script.Parent.plr2.Text == friend.UserName or not script.Parent.plr3.Text == friend.UserName or not script.Parent.plr4.Text == friend.UserName or not script.Parent.plr5.Text == friend.UserName or  not script.Parent.plr6.Text == friend.UserName or not script.Parent.plr7.Text == friend.UserName or not script.Parent.plr8.Text == friend.UserName or not script.Parent.plr9.Text == friend.UserName or not script.Parent.plr10.Text == friend.UserName or not script.Parent.plr11.Text == friend.UserName or not script.Parent.plr12.Text == friend.UserName or not script.Parent.plr13.Text == friend.UserName or not script.Parent.plr14.Text == friend.UserName or not script.Parent.plr15.Text == friend.UserName or not script.Parent.plr16.Text == friend.UserName or not script.Parent.plr17.Text == friend.UserName then 
--r would be the text chosen in the if statement 
	r.Name = friend.UserName
	r.Text = friend.UserName 
	r.Visible = true
else 
	print("error getting players friend")
end
end
end

Sorry if this is confusing, I need help on this and it would be appreciated if you could help!

So I have a question, why are you checking if the text changes in the first place, and when do you want the changes in text to happen?

  • Are you checking every single rr because you want all of them to equal just one piece of text?

  • Do you want to keep the text from each rr changing?

  • Do you want to change this text on command, but don’t want to spend extra computation changing each one?

In other words, what is the context of your situation? How would you be using your code block above?

Here is what I am trying to achieve, this is a much better example.

local player = game.Players.LocalPlayer
 local dict = player:GetFriendsOnline(200)
 for i,friend in pairs(dict) do
	print(friend.UserName.." from this script")
if friend then
if not script.Parent.plr.Text == friend.UserName or not script.Parent.plr2.Text == friend.UserName or not script.Parent.plr3.Text == friend.UserName or not script.Parent.plr4.Text == friend.UserName or not script.Parent.plr5.Text == friend.UserName or  not script.Parent.plr6.Text == friend.UserName or not script.Parent.plr7.Text == friend.UserName or not script.Parent.plr8.Text == friend.UserName or not script.Parent.plr9.Text == friend.UserName or not script.Parent.plr10.Text == friend.UserName or not script.Parent.plr11.Text == friend.UserName or not script.Parent.plr12.Text == friend.UserName or not script.Parent.plr13.Text == friend.UserName or not script.Parent.plr14.Text == friend.UserName or not script.Parent.plr15.Text == friend.UserName or not script.Parent.plr16.Text == friend.UserName or not script.Parent.plr17.Text == friend.UserName then 
--r would be the text chosen in the if statement 
	r.Name = friend.UserName
	r.Text = friend.UserName 
	r.Visible = true
else 
	print("error getting players friend")
end
end
end



1 Like

This is a bad approach. You could loop through them and check, but with your current method you’d have to do if not rr1.Text == textiwant then rr1.Text = textiwant ... A better approach would be with a loop:

local scriptParent = script.Parent -- no need to constantly check the parent, just use a variable
for i = 1, 5 do
	local currentRR = scriptParent:FindFirstChild("rr" .. tostring(i))
	if currentRR and currentRR.Text ~= textiwant then
		currentRR.Text = textiwant
		-- do whatever
	end
end

edit: currRR → currentRR (typo)

1 Like

Didn’t work, this isn’t going to be implementable in the script I provided to @goldenstein64 .

 local player = game.Players.LocalPlayer
 local dict = player:GetFriendsOnline(200)
 for i,friend in pairs(dict) do
	print(friend.UserName.." from this script")
if friend then
if not script.Parent.plr.Text == friend.UserName or not script.Parent.plr2.Text == friend.UserName or not script.Parent.plr3.Text == friend.UserName or not script.Parent.plr4.Text == friend.UserName or not script.Parent.plr5.Text == friend.UserName or  not script.Parent.plr6.Text == friend.UserName or not script.Parent.plr7.Text == friend.UserName or not script.Parent.plr8.Text == friend.UserName or not script.Parent.plr9.Text == friend.UserName or not script.Parent.plr10.Text == friend.UserName or not script.Parent.plr11.Text == friend.UserName or not script.Parent.plr12.Text == friend.UserName or not script.Parent.plr13.Text == friend.UserName or not script.Parent.plr14.Text == friend.UserName or not script.Parent.plr15.Text == friend.UserName or not script.Parent.plr16.Text == friend.UserName or not script.Parent.plr17.Text == friend.UserName then 
--r would be the text chosen in the if statement 
	r.Name = friend.UserName
	r.Text = friend.UserName 
	r.Visible = true
else 
	print("error getting players friend")
end
end
end

How is it not implementable? Just get rid of your if statements and replace it with the loop. If you need to have the else statement if all of the text boxes match, then you can just set a variable to check if any of the textboxes matched:

local scriptParent = script.Parent -- no need to constantly check the parent, just use a variable
local noMatches = true
for i = 1, 5 do
	local currentRR = scriptParent:FindFirstChild("rr" .. tostring(i))
	if currentRR and currentRR.Text ~= textiwant then
		noMatches = false
		currentRR.Text = textiwant
		-- do whatever
	end
end

if noMatches then
	print("error getting players friend")
end
1 Like

This isn’t implementable. I need to have the friends username, not textiwant.

Why can’t you just replace “textiwant” with friend.UserName? The textiwant variable was just the example you used in your original post before you edited it.

1 Like

I remade it like you said,

local player = game.Players.LocalPlayer
local scriptParent = script.Parent
local noMatches = true
 local dict = player:GetFriendsOnline(200)
 for i,friend in pairs(dict) do
for i = 1, 5 do
	local currentRR = script.Parent:FindFirstChild("plr" .. tostring(i))
	if currentRR ~= friend.UserName then
		noMatches = false
		currentRR.Text = friend.UserName
			end
end
if noMatches then
	print("error getting players friend")
end
end

And an error formed;
[attempt to index local ‘currentRR’ (a nil value)

That’s because you got rid of the part of the script that checks if it exists. I have if currentRR and currentRR.Text ~= textiwant then because the if currentRR and won’t continue the if statement if currentRR is nil.

1 Like

You would have to name the first plr label’s name plr1, but I think this could work? At least it makes the if statement shorter.

local NUM_LABELS = 17

local Names = {}
for i = 1, NUM_LABELS do
	local plrLabel = script.Parent["plr"..i]
	Names[plrLabel.Text] = true
	local lastText = plrLabel.Text
	plrLabel:GetPropertyChangedSignal("Text"):Connect(function()
		Names[lastText] = nil
		Names[plrLabel.Text] = true
		lastText = plrLabel.Text
	end)
end

local player = game.Players.LocalPlayer
local dict = player:GetFriendsOnline(NUM_LABELS)
for i, friend in pairs(dict) do
	print(friend.UserName.." from this script")
	if friend and not Names[friend.UserName] then
		local plrLabel = script.Parent["plr"..i]
		plrLabel.Text = friend.UserName
	end
end

Actually, probably not because the way it chooses the username and the way it finds the label to change is different.

2 Likes

[ attempt to index local ‘plrLabel’ (a nil value)]

Whoops, you’ll have to do that with my code too. That’s probably where the issue was earlier.

1 Like

My bad, both of your code should work if I change the first one from “plr” to “plr1”

1 Like

As I said, it worked. I’ll be making this as a solution.

1 Like