How do I check if the mouse is on same model?

In short, I complain about the function being called again when the mouse moves over the same model.

Video:

Script:

local uiInput = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local MouseInDisplay, HoldInDisplay = false, false
local currentX
local VPFcam = Instance.new("Camera")
VPFcam.Parent = script.Parent.ViewportFrame
VPFcam.CFrame = CFrame.new(0, 0, 15)
script.Parent.ViewportFrame.CurrentCamera = VPFcam
VPFcam.FieldOfView = 37

local lastTarget

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

local yazi = script.Parent.Metin
local arkaplan = script.Parent.Parent.Arka_Plan

function updatePos()
	local target = mouse.Target
	if target and target:FindFirstChild("Sergilenebilir") then
		if lastTarget then
			lastTarget:Destroy()
		end
		local newModel = target:Clone()
		newModel.Parent = script.Parent.ViewportFrame.WorldModel
		newModel.CFrame = CFrame.new(Vector3.new(0, 0, -0.4))
		lastTarget = newModel
		arkaplan.ImageTransparency = 0.22
		yazi.Text = target.Sergilenebilir.Value
		yazi.Visible = true
		print("1")
		
	else
		print("0")
		if lastTarget then
			lastTarget:Destroy()
			arkaplan.ImageTransparency = 1
			yazi.Visible = false
			lastTarget = nil
		end
	end
end

function rotateModel()
	while true do
		wait(0.001)
		if lastTarget then
			local rotation = CFrame.fromEulerAnglesXYZ(0, math.rad(3), 0)
			lastTarget.CFrame = lastTarget.CFrame * rotation
		end
	end
end

mouse.Move:Connect(updatePos)

updatePos()
rotateModel()


Thanks for help!

        if target == lastTarget then
            return
        end
local uiInput = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local MouseInDisplay, HoldInDisplay = false, false
local currentX

local VPFcam = Instance.new("Camera")
VPFcam.Parent = script.Parent.ViewportFrame
VPFcam.CFrame = CFrame.new(0, 0, 15)
script.Parent.ViewportFrame.CurrentCamera = VPFcam
VPFcam.FieldOfView = 37

local lastTarget

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

local yazi = script.Parent.Metin
local arkaplan = script.Parent.Parent.Arka_Plan


local function typeText(text)
    for i = 1, #text do
        yazi.Text = text:sub(1, i)
        wait(0.1)
    end
end


function updatePos()
    local target = mouse.Target
    if target and target:FindFirstChild("Sergilenebilir") then
        if target == lastTarget then
            return
        end
        if lastTarget then
            lastTarget:Destroy()
        end
        local newModel = target:Clone()
        newModel.Parent = script.Parent.ViewportFrame.WorldModel
        newModel.CFrame = CFrame.new(Vector3.new(0, 0, -0.4))
        lastTarget = newModel
        arkaplan.ImageTransparency = 0.22
        local fullText = target.Sergilenebilir.Value
        yazi.Text = "" -- Temizleme
        yazi.Visible = true
        typeText(fullText)
    else
        print("0")
        if lastTarget then
            lastTarget:Destroy()
            arkaplan.ImageTransparency = 1
            yazi.Visible = false
            lastTarget = nil
        end
    end
end

function rotateModel()
    while true do
        wait(0.001)
        if lastTarget then
            local rotation = CFrame.fromEulerAnglesXYZ(0, math.rad(3), 0)
            lastTarget.CFrame = lastTarget.CFrame * rotation
        end
    end
end

mouse.Move:Connect(updatePos)

rotateModel()
1 Like

Still the same problem persists.

Well in your script you will delete the model in the Viewport frame everytime you move the mouse, not mattering if the model is already in the viewport.

Maybe this works:

local uiInput = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local MouseInDisplay, HoldInDisplay = false, false
local currentX
local VPFcam = Instance.new("Camera")
VPFcam.Parent = script.Parent.ViewportFrame
VPFcam.CFrame = CFrame.new(0, 0, 15)
script.Parent.ViewportFrame.CurrentCamera = VPFcam
VPFcam.FieldOfView = 37

local lastTarget

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

local yazi = script.Parent.Metin
local arkaplan = script.Parent.Parent.Arka_Plan

function updatePos()
	local target = mouse.Target
	if target and target:FindFirstChild("Sergilenebilir") then
		if target.Name == lastTarget.Name then
			return
        else
            lastTarget:Destroy()
		end
		local newModel = target:Clone()
		newModel.Parent = script.Parent.ViewportFrame.WorldModel
		newModel.CFrame = CFrame.new(Vector3.new(0, 0, -0.4))
		lastTarget = newModel
		arkaplan.ImageTransparency = 0.22
		yazi.Text = target.Sergilenebilir.Value
		yazi.Visible = true
		print("1")
		
	else
		print("0")
		if lastTarget then
			lastTarget:Destroy()
			arkaplan.ImageTransparency = 1
			yazi.Visible = false
			lastTarget = nil
		end
	end
end

function rotateModel()
	while true do
		wait(0.001)
		if lastTarget then
			local rotation = CFrame.fromEulerAnglesXYZ(0, math.rad(3), 0)
			lastTarget.CFrame = lastTarget.CFrame * rotation
		end
	end
end

mouse.Move:Connect(updatePos)

updatePos()
rotateModel()