Change GUI Text when Part is clicked

Im trying to make a counter Gui for every time the part is clicked, yet for the life of me I can’t find out why the counter isn’t going up. Here is what I have so far:

local target = game.Workspace.Target
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = target

local counter = script.Parent --This is the TextLable Gui
local clickCounter = 0
counter.Text = clickCounter

ClickDetector.MouseClick:Connect(function(ClickDetector)
	clickCounter = clickCounter + 1
	counter.Text = clickCounter
end)

In my head this all seems right but the counter doesn’t ever change on screen. the script is a local script in the text itself. If you have any idea how to fix this I would greatly appreciate it, but please make its simple enough for me to understand because I am still learning the basics of Lua here.

Thanks!!

Hmm, where is the script located and what type of script is it?

Try making it a script. Or maybe try changing where its located (You will have to change the script if so.)

I don’t think that’s the right way to use TextBox

my apologies, I meant TextLabel

Insert:
Int value under replicated Storage.
Local script under the GUI.
Script under click detector.

Local script

local val = game.ReplicatedStorage:WaitForChild("Value")
script.Parent.counter.Text = val.Value

val.GetPropertyChangedSignal("Value"):Connect(function()
    script.Parent.counter.Text = val.Value
end)

Script:

local val = game.ReplicatedStorage:WaitForChild("Value")

script.Parent.MouseClick:Connect(function()
    val.Value = val.Value + 1
end)

If it’s not working:
Check if theres GUI that has is a “counter” text label.
Check that an int value is inserted under replicated storage.

I appreciate the feedback and I did attempt this, with sadly no success, triple checking everything was inserted correctly and in the right spots. I havn’t learned much about replicated storage yet though so this is not really in my range of comprehension as of now in fixing it myself sadly. Thank you for the feedback though, any other suggestions are always welcome!

1 Like

Can you show us the explorer?

30char

the script under target is just a script to make the target move when clicked. I found that something is wrong with the output in general because it wont even print a string when i click on the part, something seems a bit off

1 Like

Insert a remote event into the ReplicatedStorage and try this out:

-- Click script
local target = script.Parent
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = target
local remoteEvent = game.ReplicatedStorage["your remote name here"]

ClickDetector.MouseClick:Connect(function(player)
	remoteEvent:FireClient(player)
end)

-- CounterGui Local Script
local remoteEvent = game.ReplicatedStorage["your remote name here"]
local counter = script.Parent --This is the TextLable Gui
local clickCounter = 0
counter.Text = clickCounter

remoteEvent.OnClientEvent:Connect(function()
	clickCounter = clickCounter + 1
	counter.Text = clickCounter
end)

If you don’t understand the remote event, go search it on wiki, it is helpful in communicating between the client and the server.

5 Likes

Edit: That’s one way. The other way is to delete the script in the counter and try this script:

local target = script.Parent
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = target
local clickCounter = 0

ClickDetector.MouseClick:Connect(function(player)
    local counter = player.PlayerGui.ScreenGui.Frame.Counter
    clickCounter = clickCounter + 1
    counter.Text = clickCounter    
end)

I dont see other problem then the thing when u are setting the gui text. As u can see u are just changing variable instead of real gui,
Insteaf of counter.Text u should do script.Parent.Text also u should might consider putting clickCounter into string…
script.Parent.Text = tostring(clickCounter)

Local script needs to be under the ScreenGui.

just so i can understand, is this remote event allowing 2 different scripts to communicate with each other?

Yes but not enough, it’s for communicating between the client and the server.
The normal script: Server
The local script: Client

The wiki explain better than me: Client-Server Model

1 Like