How to make the brick change color when player hover mouse on it

I have a brick:


I want it to change color every time I hover the mouse on it and return to the old color if I’m not hovering my mouse on it anymore.

Problem is, I don’t know how to detect when a player is hovering their mouse on the part.

can someone help me, please?

1 Like
local plr =  game.players.LocalPlayer

local Mouse = plr:GetMouse()
local currentColor = --ur parts color here
local hovering = false

if Mouse.Target:IsA("Part")  and Moust.Target.Name == "parts name here" then
      Mouse.Target.Color = Color3.Random()
      hovering = true
      if hovering == false then
            Mouse.Target.Color = currentColor
      end
end


2 Likes

NO wait, I just tested the code and there is a bug. Lemme fix it, sorry

Fixed it, just add in a click Detector in your part and paste this in starterCharacter Scripts (in a local Script)

local plr =  game.Players.LocalPlayer
local part = game.Workspace.Part

local currentColor = game.Workspace.Part.BrickColor


part.ClickDetector.MouseHoverEnter:Connect(function()
	part.BrickColor = BrickColor.Random()
end)

part.ClickDetector.MouseHoverLeave:Connect(function()
	part.BrickColor = currentColor
end)

Mouse.Target is kinda outdated which explains why it isnt working

Mouse target does work, the problem was that @Zenqpa was not adding a connection to check if the mouse got moved, so he only checked once. Here is a working script(put in localscript in starterplayerscript):


local NORMAL_COLOR = Color3.fromRGB(255, 85, 0)

local HOVER_COLOR = Color3.fromRGB(255, 0, 0)

local Part = workspace:WaitForChild("Part") -- put your part here, this is for the test.

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

Part.Color = NORMAL_COLOR

mouse.Move:Connect(function()
	if mouse.Target == Part then
		Part.Color = HOVER_COLOR
	else
		Part.Color = NORMAL_COLOR
	end
end)

Oh yes, I forgot that, but Mouse.Target is Kinda outdated so yeah

local part = game.Workspace.Part
local originalColor = part.Color
local hoverColor = Color3.fromRGB(0, 0, 0)

local clickDetector = Instance.new("ClickDetector", part)
clickDetector.CursorIcon = "rbxassetid://" -- if you don't want a special cursor when hovering


clickDetector.MouseHoverEnter:Connect(function()
	part.Color = hoverColor
	
end)

clickDetector.MouseHoverLeave:Connect(function()
	part.Color = originalColor
	
end)

This is in a local script

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player.GetMouse()
local oldColor
local oldPart

local function rand()
	return math.random(0, 255)
end

mouse:GetPropertyChangedSignal("Target"):Connect(function()
	oldPart.Color = oldColor
	local mouseTag = mouse.Target
	if mouseTag:IsA("Part") then
		oldPart = mouseTag
		oldColor = mouseTag.Color
		mouseTag.Color = Color3.new(rand()/255, rand()/255, rand()/255) --random color when hovered
	end
end)

This only fires whenever the mouse hovers over a new part, unlike the other implementation which fires whenever the mouse is moved (even if already hovered over the part).

There is a slight problem with @Limited_Unique idea. First he wrote the function wrong using a dot and not a :

Also my testing with the script shows that getpropertychangedsignal does not work with mouse Target so I had to make my own detection script and here is the final result:

local NORMAL_COLOR = Color3.fromRGB(255, 85, 0)

local HOVER_COLOR = Color3.fromRGB(255, 0, 0)

local Part = workspace:WaitForChild("Part") -- put your part here, this is for the test.

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

local rs = game:GetService("RunService")

Part.Color = NORMAL_COLOR

local lastTarget = nil
rs.Heartbeat:Connect(function()
	if lastTarget ~= mouse.Target then
		lastTarget = mouse.Target
		print("target changed")
		if mouse.Target == Part then
			Part.Color = HOVER_COLOR
		else
			Part.Color = NORMAL_COLOR
		end
	end
end)

My script does work and it does have a colon operator?

Then what are those two edits?

Not sure, but I typically edit the majority of my posts.

Very sorry for the bump, but where would I be able to put this script? In a local or server script

In a local script in starter character scripts should be good!

1 Like