How can I detect whenever the player stops hovering over a model?

I want to detect when a player stops hover a model, I have the model saved in a variable lastTarget. I can detect hovering over it, or another model, but I can’t figure out how to detect whenever it stops hovering:

video-20230815.wmv (278,8 KB)

Here’s my LocalScript:

local function ApplyHighlight(model)
	local highlight = Instance.new("Highlight")
	highlight.Parent = model
	highlight.OutlineColor = Color3.new(1,0,0)
	
	lastRender = highlight
end

RunService.RenderStepped:Connect(function()
	if canRender then
		local mousePosition = UserInputService:GetMouseLocation()
		local mouseRay = currentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y)

		local filterTable = {}
		for i, item in pairs(game.Workspace.Items:GetChildren()) do
			if item and item:IsA("Model") and item:FindFirstChild("Owner").Value == player.Name and not table.find(filterTable, item) then
				table.insert(filterTable, item)
			end
		end

		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Include
		raycastParams.FilterDescendantsInstances = filterTable

		local raycastResult = game.Workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)

		if raycastResult and raycastResult.Instance and not raycastResult.Instance:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Highlight") then
			if not lastRender then
				local model = raycastResult.Instance:FindFirstAncestorWhichIsA("Model")
				ApplyHighlight(model)
				lastTarget = model
			else
				lastRender:Destroy()
				lastRender = nil
			end
		end
	else
		if lastRender then
			lastRender:Destroy()
			lastRender = nil
		end
	end
end)
1 Like
local Stopped = Instance.new("BindableEvent")

...
				...
				Stopped:Fire()
				lastRender:Destroy()
			...
			Stopped:Fire()
			lastRender:Destroy()
...

tool.Equipped:Connect(Equipped)
tool.Unequipped:Connect(Unequipped)

Stopped.Event:Connect(function()
	print("Stopped!")
end)

I just want to destroy the highlight (lastRender), but it doesn’t work beacuse the raycast will ignore anything that isn’t an item, the only way I thought of doing it was by creating another raycast that excludes the items, but I wanted to know if there was a better way

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