Detect if Mouse.Target has changed

Trying to detect when the Mouse.Target changes parts. Each part has the exact same name, just different parts.

local RenderStepped = RunService.RenderStepped:Connect(function()
	if not Mouse.Target then return end
			
	if Mouse.Target.Name ~= 'Wall' then return end

	local Texture = Mouse.Target:FindFirstChild('Texture')
	if not Texture then return end
	
	local OriginalTexture = Texture.Texture
	
	Texture.Texture = item.Texture
end)

Cause basically, what I want to happen, if when the player hovers over the wall, it changes the texture (which it is doing atm) however, when the mouse leaves said wall, the wall stays the new texture, instead of reverting back to the old texture (OriginalTexture)

3 Likes

Use a variable like LastPart to keep track of the previous part. No need for checking the name.

so


local LastPart = nil

local RenderStepped = RunService.RenderStepped:Connect(function()
	if Mouse.Target ~= nil then
        if Mouse.Target ~= LastPart then
       -- LastPart = Mouse.Target
       -- use this variable for reverting back to previous texture, once it's reverted then change the variable.
        end
        else
        LastPart = nil
    end
end)

I left out the texture stuff, but it can easily be implemented

2 Likes
if Mouse.Target ~= LastPart then
      LastPart = Mouse.Target

	  LastPart.Texture.Texture = OriginalTexture
else
	LastPart = nil
end

It didn’t work/nothing changed :confused:
Full script with a few edits (still not working)

local RenderStepped = RunService.RenderStepped:Connect(function()
			if not Mouse.Target then return end
			
			if Mouse.Target.Name ~= 'Wall' then return end
			
			local Texture = Mouse.Target:FindFirstChild('Texture')
			if not Texture then return end
				
			local OriginalTexture = Texture.Texture
			
			Texture.Texture = item.Texture
			
			LastPart = Mouse.Target
			
			if Mouse.Target ~= LastPart then
       			LastPart = Mouse.Target

				LastPart.Texture.Texture = OriginalTexture
			end
		end)
1 Like

You’re setting your LastPart variable right before checking if Mouse.Target isn’t equal to LastPart – remove that line before your if-statement.

local RenderStepped = RunService.RenderStepped:Connect(function()
			if not Mouse.Target then return end
			
			if Mouse.Target.Name ~= 'Wall' then return end
			
			local Texture = Mouse.Target:FindFirstChild('Texture')
			if not Texture then return end
				
			local OriginalTexture = Texture.Texture
			
			Texture.Texture = item.Texture
			
			if Mouse.Target ~= LastPart then
       			LastPart = Mouse.Target

				LastPart.Texture.Texture = OriginalTexture
			end
		end)
1 Like

Didnt change anything. When the mouse leaves the part, the Texture still stays the same

I just sort of skimmed over all this – let me read further and adjust accordingly. Still make sure that line is removed because that’s practically overriding what you’re wanting to check. :stuck_out_tongue:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local rs = game:GetService('RunService')

local LastPart = nil
local OriginalTexture = nil

rs.RenderStepped:Connect(function()
	local tar = mouse.Target
	if tar then
		if tar.Name == 'Wall' then
			local texture = mouse.Target:findFirstChild('Texture')
			if texture then
				
				if not LastPart then
					LastPart = tar
					OriginalTexture = texture.Texture
					texture.Texture = item.Texture
				end
				
				if tar ~= LastPart then
					LastPart:WaitForChild('Texture').Texture = OriginalTexture
					LastPart = nil
				end
			end
		end
	end
end)

This should do the trick :slight_smile:

6 Likes

Would you by any chance know why this is occuring?

com-video-to-gif

Basically, works, but when you change colors, it still stays the same original color. You have to press the button twice for it to actually change. Even tho clicking it once is setting the ‘selected’ color to the new color.

Tried adding this above it, thinking it would stop the render from happening, but did nothing

if RenderStepped then
	RenderStepped:Disconnect()
end
1 Like