Click detector not updating value via script

I have a script that when hovering over a part, shows the name of the part.
I then made it so a ClickDetector gets inserted into the part my mouse is hovering over.

I want to make it so that when clicking that part, a TextLabel’s value shows the part name, yet I’m struggling to do that since I can’t find the specific ClickDetector since the part I select is given a random name.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local values = plr.PlayerGui.Input.TextBox
local click = Instance.new("ClickDetector")
local c

mouse.Move:Connect(function()
	local target = mouse.Target

	for _,blocks in pairs(workspace.Blocks:GetChildren()) do
		if target == blocks then

			if not(blocks:FindFirstChild("ClickDetector")) then
				c = click:Clone()
				c.Parent = target
				values.id.TextTransparency = 0
				values.PartName.Text = target.Name
			end

		else
			if blocks:FindFirstChild("ClickDetector") then
				c:Destroy()
				values.id.TextTransparency = 1
				values.PartName.Text = ""
			end
		end
	end
end)

workspace.Blocks.Part.ClickDetector.MouseClick:Connect(function()
	values.idSelected.Text = values.PartName
end)

(the parts the ClickDetectors get put in are given random names using HttpService, so the target is used to find the part regardless of the name, yet due to this, if I have multiple parts, I need it to find the ClickDetector in the part regardless of the name)

Is there any way I can fix this issue?

1 Like

Here’s how I would do it. Insert a script inside your click detector that transfers it’s name to a int value or a strong value, id recommend using remotes to send to the int value or string value, then just grab tje value of that and put it on a text label

1 Like

Screen Shot 2023-08-17 at 1.00.33 AM

This is an example of the randomized name I forgot to mention.
It has a random 7 character string for every part cloned and put in that folder, and when hovered over, it puts a ClickDetector in that respective part.

1 Like

while wait() do
game.ReplicatedStorage.RemoteEvent:FireServer(function(script.Parent.Name)
remoteevent.OnServerEvent:Connect(script.parent.Name)
label.text = script.parent.name

May I see the script in explorer?

Screen Shot 2023-08-17 at 1.04.58 AM

Is that a Script or LocalScript?

You need to run the ClickDetector scripts in Script set to Client.

Use a remote event to transfer the data to a script

It looks like a local script to me

Yes, the script is in a LocalScript

Move your code to a Script and then in propertioes set the runcontext to Client

You can just make the .MouseClick connection when you clone the click detector into the part.

1 Like

I think your issue is that you’re not hooking up the MouseClick connection on cloning (as @txcIove said)

1 Like

Like putting the whole function in that section? Or only the MouseClick, I’m not sure what you mean

			if not(blocks:FindFirstChild("ClickDetector")) then
				c = click:Clone()
				c.Parent = target
				values.id.TextTransparency = 0
				values.PartName.Text = target.Name
                c.MouseClick:Connect(function() 
                      values.idSelected.Text = values.PartName
                end)
			end

Basically you need to hook up the MouseClick Connection to the newly cloned ClickDetector

1 Like

Nevermind, I forgot the .Text after PartName

Is values.PartName a string
If its a TextLabel then you need to use TextLabel.Text

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