ClickDetector - ScreenGui not found

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want it so that when you click on this part, the text on the UI changes.

  2. What is the issue? Include screenshots / videos if possible!
    I have got an error.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked at a post on here, but that didn’t help me.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local TextLabel = workspace.ScreenGui.TextLabel

function onClicked()
	if TextLabel.Text == "Insert Master Key" then 
		TextLabel.Text = "Remove Master Key"

	else

		if  TextLabel.Text == "Remove Master Key" then
			TextLabel.Text = "Insert Master Key"
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.


Of course you got an error because as you see in the Explorer, there is no child named ‘ScreenGui’ under workspace

1 Like

Would it be StarterGui instead

No, you need to do

local player = game.Players.LocalPlayer
local textLabel = player.PlayerGui.ScreenGui.TextLabel

either make a LocalScript and put it in the TextLabel

local TextLabel = script.Parent

function onClicked()
  local Insert, Remove = "Insert Master Key", "Remove Master Key" -- avoid typing it out so many times

  if TextLabel.Text == Insert then 
    TextLabel.Text = Remove
  elseif TextLabel.Text == Remove then
    TextLabel.Text = Insert
  end
end

game.Workspace:WaitForChild("MasterKeyInsert").ClickDetector.MouseClick:connect(onClicked)

or use ServerScript that’s in the part and just change the TextLabel of the player that clicked the ClickDetector

function onClicked(Player)
  local TextLabel = Player.PlayerGui.ScreenGui.TextLabel
  local Insert, Remove = "Insert Master Key", "Remove Master Key"

  if TextLabel.Text == Insert then 
    TextLabel.Text = Remove
  elseif TextLabel.Text == Remove then
    TextLabel.Text = Insert
  end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

you could also replace the if statements with this if you wanted it shorter

local Insert, Remove = "Insert Master Key", "Remove Master Key"
TextLabel.Text = (TextLabel.Text == Insert) and Remove or Insert
-- if text was 'Insert' then switch to 'Remove', otherwise switch to 'Insert'

Thanks for your help, I don’t often use clickdetectors so it’s not my strong point.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.