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

Help with TextButton MouseClick error?

Asked by
Cvbza 12
6 years ago

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

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
1
THX it worked! But what is "script.Parent"? Cvbza 12 — 6y
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). User#19524 175 — 6y
1
Ok cool! So if I had a Script inside ServerScriptService I can access it with "script.Parent"? If so cool. Cvbza 12 — 6y
0
Yup. User#19524 175 — 6y
Ad

Answer this question