Part that changes color when you click on it

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear! Part changes to random color when clicked

  2. What is the issue? Include screenshots / videos if possible! I am not writing the script correctly…

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    while game.Workspace.Part.ClickDetector.MouseClick.OnClick
    do game.Workspace.Part.BrickColor = BrickColor.Random()
    End (where did I go wrong pls help)

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you! Part changes to random color each time is is clicked…

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I recommend reading up on how events work: https://developer.roblox.com/en-us/articles/events

Edit for clarity: MouseClick.OnClick isn’t a thing, and while whatever.ClickDetector.MouseClick do would just run for as long as the event exists, not when it fires, which would be always.

3 Likes

Do you have a click detector? Anyway the script is wrong. Put a script in a click detector:

    script.Parent.MouseClick:Connect(function()
        script.Parent.Parent.BrickColor = BrickColor.Random() -- can be any color
end)
4 Likes

It’s not necessarily wrong. It’s just another method of doing it. Both ways can work just as fine.

No, his method is absolutely wrong. It won’t work at all.

3 Likes

wait nvm my brain turned off, yeah it’s wrong.
Try what @HabibiBean did.


Or if you still prefer to use the while do method, it can work. It just needs a little fixing.

while workspace.Part.ClickDetector.MouseClick:Wait() do
workspace.Part.BrickColor = BrickColor.Random()
end

You should also either use it from a script inside of the click detector or make a variable to the specific part, otherwise you’ll have trouble actually finding it.

1 Like

This is easy.

-- workspace.Part, wait what part? Just place the script inside the ClickDetector
-- solution is made by HabibiBean earlier
script.Parent.MouseClick:Connect(function()
    script.Parent.Parent.BrickColor = BrickColor.Random()
end)

You should probably name the part you want the clicking to work on. Or place the script inside the part. It’s all up to you to decide.

1 Like

I feel like going with an event is still the better choice, since this is a little too close for comfort to the while wait do idiom (and is also over-complicating things). This would work, yes, but it seems like a bad practice.

3 Likes

You’re probably right. I just wanted to provide an option though, not encourage the practice.

1 Like

What I’d do here is call the MouseClick and at the same time get the Mouse.Target what object the mouse is clicking and basically just change the color. Here’s an example

Summary

local Mouse = game.Players.LocalPlayer:GetMouse()
Mouse.ButtonClick:Connect(function()
local Target = Mouse.Target – If your part has no children
Target.BrickColor = BrickColor.new(“YourColorHere”)
game.ReplicatedStorage.ChangeAnyBrickColor:FireServer(Target:GetFullName)
end)

Server:
remote.OnServerEvent:Connect(function(Player, Path)
local Part = game[Path]
Part.BrickColor = BrickColor.new(“ColorHere”)

end)

Personally, I prefer to mimick the client’s behaviour of changing the color from both client and server, it may seem inconvenient here but I like to code securely.

1 Like

Don’t put a while loop, use an event connection.

game.Workspace.Part.ClickDetector.MouseClick.OnClick:Connect(function()
	 game.Workspace.Part.BrickColor = BrickColor.Random()
end