My hover on brick script keeps looping

So I’m trying to make a script that turns bricks blue whenever its being hovered on. It does work but it loops so it flickers. Heres the script

LocalScript:

local mouse = plr:GetMouse()
local hoverable = {workspace.BRICK2, workspace.BRICK}
local HOVER_COLOR = BrickColor.Blue()
local BRICKCOLORS = {}
local hover = false

for i, brick in ipairs(hoverable) do
	BRICKCOLORS[brick.name] = brick.BrickColor
end

mouse.Move:Connect(function()
	if table.find(hoverable, mouse.Target) then
		if (mouse.Target.BrickColor == BRICKCOLORS[mouse.Target.Name] and hover == false) then
			mouse.Target.BrickColor = HOVER_COLOR
			hover = true
		else
			mouse.Target.BrickColor = BRICKCOLORS[mouse.Target.Name]
			hover = false
		end
	end
end) ```
1 Like

Found a solution for it. How to make the brick change color when player hover mouse on it - #9 by Limited_Unique By AC_Starmarine

Just change if mouse.Target == Part then to table.find(hoverable, mouse.Target) in his script

2 Likes

Okay so do i replace the whole script or what do i replace?

local NORMAL_COLORS = {}

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

local Parts = {workspace.BRICK2, workspace.BRICK}

for i, brick in ipairs(Parts) do
NORMAL_COLORS[brick.name] = brick.Color
end

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

local rs = game:GetService(“RunService”)

local lastTarget = nil
rs.Heartbeat:Connect(function()
if lastTarget ~= mouse.Target then
lastTarget = mouse.Target
print(“target changed”)
if table.find(Parts, mouse.Target) then
mouse.Target.Color = HOVER_COLOR
else
mouse.Target.Color = NORMAL_COLORS[mouse.Target.Name]
end
end
end)

1 Like

What about the Part.Color thing?

Could you please elaborate? I changed the whole script how it needs to be. You can replace it.

1 Like

Oop! I found my mistake! Hold on!

1 Like

So, i tried to do this if table.find(Parts, mouse.Target) then Parts[mouse.Target.Name].Color = HOVER_COLOR else Parts[lastTarget.Name].Color = NORMAL_COLORS[mouse.Target.Name] end but ended up not working I was wondering how do i exactly fix this? The error was Players.Seth6131926.PlayerGui.LocalScript:26: attempt to index nil with 'Name' oh nevermind then!

So hovering on it works now, but unhovering doesn’t heres the error Players.Seth6131926.PlayerGui.LocalScript:24: attempt to index nil with 'Name'

Wait nevermind I tried storing the individual rgb color codes for i, brick in ipairs(Parts) do NORMAL_COLORS[brick.name] = { brick.Color.R, brick.Color.G, brick.Color.B, } end and apply it like this lastTarget.Color = Color3.fromRGB(NORMAL_COLORS[lastTarget.name].R, NORMAL_COLORS[lastTarget.name].G, NORMAL_COLORS[lastTarget.name].B) but i got this error instead Players.Seth6131926.PlayerGui.LocalScript:28: attempt to index nil with 'R'

wait i’m stupid hold on

characters

Sorry for the wait!!!

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

local HOVER_COLOR = Color3.fromRGB(255, 0, 0)
local plr = game.Players.LocalPlayer
local hoverable = {workspace.BRICK2, workspace.BRICK}
local HOVER_COLOR = BrickColor.Blue()
local mouse = plr:GetMouse()
local rs = game:GetService("RunService")
local CurrentPart
rs.Heartbeat:Connect(function()
	if table.find(hoverable,mouse.Target) then
		CurrentPart = mouse.Target
		CurrentPart.BrickColor = HOVER_COLOR
	else
		if CurrentPart ~= nil then
			CurrentPart.BrickColor = BrickColor.new("Medium stone grey")
			CurrentPart = nil
		end
	end
end)
1 Like

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