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

Help with TextButton MouseClick error?

Ok so I have a Screen Gui which has a TextButton, and a ClickDetector inside the textbutton. Inside the TextButton is a Script. A regular one. Nothing happens.

local r = math.random(0,255)
local g = r
local b = g
game.StarterGui.ScreenGui.TextButton.ClickDetector.MouseClick:Connect(function()
    game.Workspace.Part.Color = Color3.fromRGB(r, g, b)
end)

1 answer

✓ Accepted answer

Click detectors only work when parented to parts, and the script must be a LocalScript, and textbuttons have their own Mouse Click event, here it is. Also, you need to get the player's PlayerGui to access this button, as the StarterGui is just a placeholder for the gui.

local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

script.Parent.MouseButton1Click:Connect(function() -- This is it.
    -- Do your code here. 
end)

4 comments

1

THX it worked! But what is "script.Parent"?

0

First, "script" is a reference to the script you're currently inside. It's like assigning a variable to an object, you can do methods, like :Destroy() and get properties (script.Parent shown in the code).

1

Ok cool! So if I had a Script inside ServerScriptService I can access it with "script.Parent"? If so cool.

0

Yup.

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