How to Revert color after changing it with mouse.Move

Hello, I am making a grid for a game. I want parts that my mouse hovers over to change color and then revert to the color it was previously when the mouse leaves the part.

However, I am unsure how to do this because I don’t want the script to revert the color of the part if the mouse is still hovering over it.

The Script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local TS = game:GetService("TweenService")
local CS = game:GetService("CollectionService")
print("THIS IS RUNNING")
mouse.Move:Connect(function()
	

	
	if CS:HasTag(mouse.Target,"Highlightable") then
		local T = mouse.Target
		local bc = T.BrickColor
		local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 0, false, 0)
		local tweenIn1 = TS:Create(mouse.Target,tweenInfo,{Color = Color3.fromRGB(175, 255, 70)})
		print(mouse.Target)
		tweenIn1:Play()
	end
end)

How it runs as of now:

I am also aware that MouseHoverEnter and MouseHoverLeave exist, but I am not sure if implementing them will be a good idea because I plan on having many of these parts in my game.

I made a code in studio that will help you, just edit this code for your needs (it’s not exactly your code but it’s the gist). Basically, I give it a current target, if the new mouse target isn’t the current target then I reset the color, then I set the current target to the new mouse target:

local Mouse = game.Players.LocalPlayer:GetMouse()
local CurrentTarget = Mouse.Target

Mouse.Move:Connect(function()
	if typeof(CurrentTarget) == "Instance" then
		if typeof(Mouse.Target) == "Instance" then
			if CurrentTarget.Name ~= Mouse.Target.Name then
				CurrentTarget.BrickColor = BrickColor.new("Really red")
				CurrentTarget = Mouse.Target
			else
				CurrentTarget.BrickColor = BrickColor.new("Really blue")
			end
		end
	else
		CurrentTarget = Mouse.Target
	end
end)

1 Like

Thanks for the response!

I tried implementing CurrentTarget into my code and am pretty satisfied with the product!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.