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
7 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.

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

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 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.

1local player = game:GetService("Players").LocalPlayer
2local playerGui = player:WaitForChild("PlayerGui")
3 
4script.Parent.MouseButton1Click:Connect(function() -- This is it.
5    -- Do your code here.
6end)
1
THX it worked! But what is "script.Parent"? Cvbza 12 — 7y
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 — 7y
1
Ok cool! So if I had a Script inside ServerScriptService I can access it with "script.Parent"? If so cool. Cvbza 12 — 7y
0
Yup. User#19524 175 — 7y
Ad

Answer this question