Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 1 year ago

The code i made that didnt work

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

4 answers

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
1 year ago

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)
0
There would be no use to using tostring when its already sending text Protogen_Dev 268 — 1y
0
Yea just in case MattVSNNL 620 — 1y
0
Just in case they send like a number? I mean it won't do anything really at all Protogen_Dev 268 — 1y
0
As good as the scripting itself is, tostring isn't required to be used and just wastes a second of typing. Protogen_Dev 268 — 1y
View all comments (2 more)
0
It doesn't really matter bro MattVSNNL 620 — 1y
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 ilovekidcity 10 — 1y
Ad
Log in to vote
0
Answered by
ryanzhzh 128
1 year ago

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.

0
He is looking for it on click, and MouseClick does have the player argument. Protogen_Dev 268 — 1y
0
Sorry but your script didnt work when I used it, I dont know whats wrong with it. ilovekidcity 10 — 1y
0
Can you show me how to make that EVERY player in the server can see the text? ilovekidcity 10 — 1y
0
you didn't say that. clearly didn't. next time say that. ryanzhzh 128 — 1y
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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

0
Can you show me how to make it so every player can see the text? ilovekidcity 10 — 1y
0
Change :FireClient(PlayerThatClicked) to :FireAllClients() Protogen_Dev 268 — 1y
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! ilovekidcity 10 — 1y
0
Just one question, how can i make it that the text has the name of the player who clicked the button? ilovekidcity 10 — 1y
View all comments (3 more)
0
Server Script: Remote:FireAllClients(PlayerThatClicked.) Protogen_Dev 268 — 1y
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" Protogen_Dev 268 — 1y
0
Alr it worked, tysm! ilovekidcity 10 — 1y
Log in to vote
0
Answered by
enes223 327 Moderation Voter
1 year ago

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

Answer this question