Get player that touched buttons name HELP

Hey everyone, so today I was working on a little script for practice, and I came up on this problem, when I click the part, the screengui should enable again and the text pops up with the name of the person that clicked it, I am not sure how to fix this because it doesn’t work. There are no errors. I think I’m suppose to use playergui, just not sure how. Here is my script.

 --//Variables//--
local plr = game.Players.LocalPlayer
local screengui = game.StarterGui.ScreenGui
local txt = game.StarterGui.ScreenGui.TextLabel
local button = game.Workspace.button
--//Main Code//--
button.ClickDetector.MouseClick:Connect(function(hit)
	if hit.Parent == plr and screengui.Enabled == false then
		screengui.Enabled = true
		txt = (hit.Parent.. "Touched the Button!")
	end
end)

if anyone knows how to fix this, please let me know, thanks!

i think hit is the player that clicked a click detector

so

txt = hit.Name.."Touched the button!"

2 Likes

Hi! So for the MouseClick event function it passes along the player variable already, so if you want the players name you can just do hit.Name to get there string name for it! you have your screengui targeting the one in StarterGui . So if the script is the child of the UI itself then I would just do script.Parent for the screengui variable

Isn’t the hit parameter a player instance? And also you can just use local player = hit:GetPlayerFromCharacter() to turn the hit parameter into a character
EDIT: Nevermind im dumb you were talking about the gui

Ok so I removed the screengui variable and made it enabled and made the background 1, so I made the text not visible, but it still does not work. Do you know why? here is my changed script…

--//Variables//--
local plr = game.Players.LocalPlayer
local txt = game.StarterGui.ScreenGui.TextLabel
local button = game.Workspace.button
--//Main Code//--
button.ClickDetector.MouseClick:Connect(function(hit)
	if hit.Parent == plr and txt.Visible == false then
		txt.Visible = true
		txt = hit.Name.. "Touched the Button!"
	end
end)
1 Like

I changed it again, I removed the if hit.Parent == plr. I changed it to if txt.Visible == false then, I made a print thing to see if it ran, it printed but nothing happened after.

--//Variables//--
local plr = game.Players.LocalPlayer
local txt = game.StarterGui.ScreenGui.TextLabel
local button = game.Workspace.button
--//Main Code//--
button.ClickDetector.MouseClick:Connect(function(hit)
	if txt.Visible == false then
		print("It's Visible!")
		txt.Visible = true
		txt = hit.Name.. "Touched the Button!"
	end
end)

again Like I stated in my first post StaterGui will not change anything for the PlayerGui which is what gets replicated to the player on join/respawn. Changing the name of the variable will not change much, can you show me what the explorer looks like so I can help with the hierarchy

oh, My apologies, I must have not gotten what you were saying. I will try to read better next time. Here is my workspace.

Roblox Snippet

local plr = game.Players.LocalPlayer
local txt = script.Parent.TextLabel
local button = game.Workspace.button
--//Main Code//--
button.ClickDetector.MouseClick:Connect(function(hit)
	if txt.Visible == false then
		print("It's Visible!")
		txt.Visible = true
		txt = hit.Name.. "Touched the Button!"
	end
end)
3 Likes

That worked, but It doesn’t show up the text that I want it to.

txt.Text = hit.Name… “Touched the Button!”

Hence how variable naming can cause confusion!

1 Like

Sorry I forgot one part

txt.Text = hit.Name.. "Touched the Button!"

Thanks for all the help! means a lot!

1 Like