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

PlayerList) Instead of grabbing the name clicked on, grabs latest made player?

Hi, I've been having troubles with a GUI not grabbing the right name. Instead of grabbing the name I clicked it does the latest instanced.new player in the list.

I am unsure of how to fix this

local userID = 1
local PlayerName = "Roblox"

local function onButtonActivated()
	PlayerName = PLRLNAM.Text
end

local function addplayerlist(plr)
	Player = Instance.new("Frame",PlayerList)
	PLRLNAM = Instance.new("TextButton",Player)
	ICo = Instance.new("ImageLabel",Player)
	Player.BackgroundColor3 = Color3.fromRGB(16, 16, 16)
	Player.BorderSizePixel = 0
	Player.Size = UDim2.new(0, 200, 0, 29)
	Player.Name = plr.Name
	--
	PLRLNAM.Name = "PLRLNAM"
	PLRLNAM.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	PLRLNAM.BackgroundTransparency = 1
	PLRLNAM.BorderSizePixel = 0
	PLRLNAM.Position = UDim2.new(0.159999996, 0, 0, 0)
	PLRLNAM.Size = UDim2.new(0, 158, 0, 26)
	PLRLNAM.Font = Enum.Font.Code
	PLRLNAM.Text = plr.Name
	PLRLNAM.TextColor3 = Color3.fromRGB(230, 230, 230)
	PLRLNAM.TextSize = 14.000
	PLRLNAM.TextXAlignment = Enum.TextXAlignment.Left
	--
	ICo.Name = "ICo"
	ICo.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	ICo.BackgroundTransparency = 1.000
	ICo.BorderSizePixel = 0
	ICo.Position = UDim2.new(0, 0, 0.103448279, 0)
	ICo.Size = UDim2.new(0, 23, 0, 23)
	ICo.Image = "http://www.roblox.com/asset/?id=413369185"
	--
	PLRLNAM.MouseButton1Click:Connect(onButtonActivated)

end

for i,v in pairs(game.Players:GetPlayers()) do
	addplayerlist(v)
end


game.Players.PlayerAdded:Connect(function(plr)
	addplayerlist(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	for i,v in pairs(PlayerList:GetChildren()) do
		if v and v.Name == plr.Name then
			v:remove()
			break
		end
	end
end)



--Testing what the value is
while wait(1) do
	print(PlayerName)
end

What I'm trying to accomplish, Player clicks on the "PLRLNAM" and sets the local PlayerName value to their name.

8 comments

0

What does plr refer to? You never specified its value, nor did you specify where it came from.

0

plr is the grabbed value from the Player tab to spawn the textbutton with the .Name of player.

0

Which is referenced...by what exactly? The issue is most likely prevalent when you're assigning the player.

0

Edited the code box to show everything I have so far.

0

uh i think it going to the newest one cos ur taking it from the function u use to create the button not the one u click. u ned find the one dat u click

0

Yep, there's your problem. It's retrieving the latest player because you are assigning it to the player passed by PlayerAdded, which is the player who most recently connected to the server. If I'm not mistaken, if this is a LocalScript, you can use LocalPlayer. PlayerList is also undefined unless you specified it outside of what you're showing us.

0

Alright, I'm quite new to scripting again since I haven't touched roblox in a good 3 years. if you can explain to me what I need to do to make this work I would greatly appreciate it. Also yes the PlayerList is defined in the GUI part of the local script

0

uh i dum :-: idk how make 0: i would probably do somthin like for _,v in pairs(PlayerList:GetChildren()) do local click = v:FindFirstChildOfClass'TextButton' click.MouseButton1Click:Connect(function() but idk if it wil work and ther probably a better way to do it xd

1 answer

Realized that I had the PLRLNAM a global variable, and did not have the onButtonActivated in the addplayerlist function

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