Ad
Log in to vote
1

Help with TextButton MouseClick error?

Asked by
Cvbza 9
7 days 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
incapaz 517
7 days ago
Edited 7 days 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 9 — 7d
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). incapaz 517 — 7d
1
Ok cool! So if I had a Script inside ServerScriptService I can access it with "script.Parent"? If so cool. Cvbza 9 — 7d
0
Yup. incapaz 517 — 7d
Ad

Answer this question



Ad