Script only sometimes finds ClickDetector

The script either works or says that “ClickDetector is not a valid member of Part” in the output

Here are the scripts:
Script inside SurfaceGui inside a part

local TextLabel = Instance.new("TextLabel")
local ClickDetectorMouse = workspace.House1.House1Furniture.Mouse.ClickDetector
local ClickDetectorKeyboard = workspace.House1.House1Furniture.Keyboard.ClickDetector
local Number = 0
TextLabel.Size = UDim2.new(0, 175, 0, 100)
TextLabel.Parent = workspace.House1.House1Furniture.Monitor.Screen.SurfaceGui
TextLabel.Text = (Number)
TextLabel.TextScaled = true

ClickDetectorMouse.MouseClick:Connect(function()
	Number += 1
	TextLabel.Text = (Number)
end)

ClickDetectorKeyboard.MouseClick:Connect(function()
	Number -= (Number)
	TextLabel.Text = (Number)
end)

Script inside Mouse

local Sound = Instance.new("Sound")
local ClickDetector = Instance.new("ClickDetector")
Sound.SoundId = "rbxassetid://1673280232"
Sound.Parent = workspace.House1.House1Furniture.Mouse
ClickDetector.MaxActivationDistance = 5
ClickDetector.Parent = workspace.House1.House1Furniture.Mouse

ClickDetector.MouseClick:Connect(function()
	Sound:Play()
end)

Script above is the same with the Keyboard but with a different parent and sound

I am trying to make a SurfaceGui that has a number go up when you click on a part and goes to zero when you click on another part

1 Like

Change it to this:

local TextLabel = Instance.new("TextLabel")
local ClickDetectorMouse = workspace.House1.House1Furniture.Mouse:WaitForChild("ClickDetector")
local ClickDetectorKeyboard = workspace.House1.House1Furniture.Keyboard:WaitForChild("ClickDetector")
2 Likes

It constantly works now, thank you! :smile:

1 Like