Gui button isn't changing the color of another part when pressed

Okay So I was making a gui text button that once it is pressed it will change the color of another part. It is simple yet I’m dumb to not make it work.

Summary

local screengui = game.StarterGui.ScreenGui

local Black = screengui.TextButton

local tv = game.Workspace.tv

local Textbutton = game.StarterGui.ScreenGui.TextButton

local clickdectecter = Instance.new(“ClickDetector”)

clickdectecter.MaxActivationDistance = 10

clickdectecter.Parent = screengui

Textbutton.MouseButton1Click:Connect(function()

tv.BrickColor = BrickColor.new(“Black”)

print(“black”)

end)

So what’s the problem saying it doesn’t work doesn’t give us enough information to help you

It don’t work. I click on it but nothing happens

Alright, try parenting the click detector to game.Workspace.tv, also are there any other errors?

Nevermind just realised your parenting to the StarterGui

1 Like

You are using the StarterGui instead of the new gui instance that is created when a player joins

Create a localscript inside of your gui and use script.Parent … etc to access your gui elements

1 Like

Can I edit it to clickdectecter.Parent = game.screengui ?

or just re-write it

Let me try that really quick. :grinning:

Im assuming your trying to parent it onto a part right ? Make the screengui parented to a part.

Like do you want the click detector to be clickable on the part?

You will also want to use Remote Events / Remote Functions to change the brick’s color from a Script

LocalScript Example:

local Gui = script.Parent
local Remote = game.ReplicatedStorage:WaitForChild("BrickColorRemote")
local Frame = Gui.Frame
local Button = Frame.BrickColorButton

Button.MouseButton1Click:Connect(function()
	Remote:FireServer(Color3.fromRGB(255,0,0))
end)

Server Script Example

local Remote = Instance.new("RemoteEvent", game.ReplicatedSotrage)
Remote.Name = "BrickColorRemote"
Remote.OnServerEvent:Connect(function(player,arg)
	game.Workspace.Part.Color = arg
end)
1 Like

I haven’t learned about remotes and stuff like that

Well I Fixed it

Summary

local gui = script.Parent

local Button = gui.TextButton

local click = game.Workspace.ClickDetector

Button.MouseButton1Click:Connect(function()

print(“Black”)

game.Workspace.tv.BrickColor = BrickColor.new(“Black”)

end)

Thanks .

Example using remotes:

Gui.rbxl (27.0 KB)

Well, Remote Rvents are unecessary for what OP needed. I don’t get why everyone wants to use Remote Events for everything, when there are simpler options - and this goes for other posts as well.

And don’t get me wrong, I love using Remote Events and know them well - but when it’s just like what OP wanted, there’s no need to use a server-side script to simply change the color of a part in the Workspace.

But anyways, I’m glad it got fixed! Have a good day/week everyone!

Without Remotes

Using Remotes


You don’t need the ClickDetector at all, and just be aware this only changes the part colour on the client who has the Gui - everyone else in the game will still see the old part colour.

Also remember to use three backticks - \`\`\` - to enclose your code to make it readable,

```
function()
(tab) print(‘hello’)
end
```

turns into

function()
   print('hello')
end
2 Likes

Oh okay I’ll use them from now

print("Thanks")

He legit said he fixed it - and it doesn’t look like he used remotes.

I didn’t use remotes because I don’t know how to use them. But I’m learning new things everyday.
also keep in Mind It’s been only a week since I’ve been learning Lua.

1 Like