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

How do I make it so if I press a click detector on a part it will change a text of a text label?

The code i made that didnt work

function OnClick()
	game.StarterGui.ScreenGui.TextLabel.Text = "works"
end
script.Parent.ClickDetector.MouseClick:connect(OnClick)

4 answers

✓ Accepted answer

Hello, I made a new script for you, I see that the other persons script works so I made a new one where you can change the text anytime very quickly!

  1. Add a RemoteEvent called ChangeText in replicatedStorage
  2. Add a Script inside your ClickDetector
  3. Add a LocalScript inside your TextLabel

Script in ClickDetector:

local replicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.MouseClick:Connect(function()
	replicatedStorage:FindFirstChild("ChangeText"):FireAllClients("Works") -- Change to the text you want!
end)

LocalScript in TextLabel:

local replicatedStorage = game:GetService("ReplicatedStorage")

replicatedStorage:FindFirstChild("ChangeText").OnClientEvent:Connect(function(text)
	script.Parent.Text = tostring(text)
end)

6 comments

0

There would be no use to using tostring when its already sending text

0

Yea just in case

0

Just in case they send like a number? I mean it won't do anything really at all

0

As good as the scripting itself is, tostring isn't required to be used and just wastes a second of typing.

0

It doesn't really matter bro

0

Protogen_Dev showed me how to change the code to what I wanted which was to put the name of the Player who clicked it as the text, so I accept this answer. Also ty Protogen_Dev for showing me how to change the code, btw im new to scripting

Really simple.

function OnClick(player: Player)
    player.PlayerGui.TextLabel.Text = "works"
end
script.Parent.ClickDetector.MouseEnter:Connect(OnClick)

You're changing it so it shows up when they respawn or anyone.

MouseEnter (NOT MOUSECLICK!) has a player argument to get the player.

4 comments

0

He is looking for it on click, and MouseClick does have the player argument.

0

Sorry but your script didnt work when I used it, I dont know whats wrong with it.

0

Can you show me how to make that EVERY player in the server can see the text?

0

you didn't say that. clearly didn't. next time say that.

The fastest way to do this would actually be via the use of remotes. Its finicky to use a server sided script to change a players UI.

First create a remote event within ReplicatedStorage, name it whatever you want. I went with ClickRemote.

Inside of a local script inside of the text box your code should look like this:

local Remote = game.ReplicatedStorage.ClickRemote

Remote.OnClientEvent:Connect(function()
	script.Parent.Text = "Text to set to"
end)

Inside of the server script you can setup your code to detect clicks and fire the remote towards the player that clicks:

local ClickDetector = script.Parent.ClickDetector
local Remote = game.ReplicatedStorage.ClickRemote

local function HandleAClick(PlayerThatClicked)
	Remote:FireClient(PlayerThatClicked)
end

ClickDetector.MouseClick:Connect(HandleAClick)

This will change the text of the UI

7 comments

0

Can you show me how to make it so every player can see the text?

0

Change :FireClient(PlayerThatClicked) to :FireAllClients()

0

Alright, it worked, tysm! Idk how to put it as the answer bc it seems to be removed but ty it worked for me!

0

Just one question, how can i make it that the text has the name of the player who clicked the button?

0

Server Script: Remote:FireAllClients(PlayerThatClicked.)

0

This is gonna be harder to explain but fits in well with what Matt said as an answer, you could change the "Works" in :FireAllClients() to (In my script) PlayerThatClicked.Name and the local script needs function() changing to function(playerName) and then script.Parent.Text = playerName .. " clicked the button"

0

Alr it worked, tysm!

hey you! have you ever heard of enes? if you are in trouble, better call enes!

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