Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Solved]How would I get a previous mouse target when the mouse changes targets?

Okay here is an example to expand on the question. The player moves their mouse over a parta, but then moves it to partb. How would I keep parta as a viable when the player changes their target?

Right now this is my script which isn't working at all

mouse.Move:Connect(function()
	if placing then
		
	if plr:FindFirstChild("ObjTarget") == nil then
		objTarg = Instance.new("ObjectValue")
		objTarg.Value = nil
		objTarg.Name = "ObjTarget"
		objTarg.Parent = plr
	end		
		
		wait()
		local mseX = mouse.Hit.X
		local mseY = mouse.Hit.Y
		local mseTarget = mouse.Target
		
		if mseTarget then
			for i,v in pairs (tycoon) do
				if v == mseTarget then
					unitTarget = v
					objTarg.Value = unitTarget
				end
			end
			objTarg.Changed:connect(function()
				previousTarget = mseTarget
				print(previousTarget)
			end)		
		end
	end
end)

I am pretty sure I didn't even get close to that script but I have been stumped for the past hour and I'm finally just going to ask.

1 comment

0

do you know what your comparing a variable to a variable when you could be using the variable itself...

1 answer

✓ Accepted answer

The short answer is to assign the current target to a previous target variable at the end of the function (but only if the current target is different from the previous target).

You should keep code that you only want to run once outside of event code. For instance, you only need to check if the player has ObjTarget once. You should never set up an event within another event, as you do with objTarg.Changed.

--I assume you have a variety of variables defined here

--Create objTarget if needed
local objTarget = plr:FindFirstChild("ObjTarget")
if not objTarget then
    objTarg = Instance.new("ObjectValue")
    objTarg.Value = nil
    objTarg.Name = "ObjTarget"
    objTarg.Parent = plr
end
local previousTarget

function IsValidTarget(target) -- returns true if the player can target the specified 'target' object
    for i, v in ipairs(tycoon) do
        if v == target then return true end
    end
    return false
end

mouse.Move:Connect(function()
    if not placing then return end -- do nothing if not placing something
    local mseX = mouse.Hit.X
    local mseY = mouse.Hit.Y
    local mseTarget = mouse.Target

    if mseTarget and IsValidTarget(target) then
        -- Update objTarget
        objTarg.Value = mseTarget
        -- Update previousTarget
        if mseTarget ~= previousTarget then -- if they are the same then the user has simply moved the mouse a bit and hasn't changed targets, so only update previousTarget if they are different
            previousTarget = mseTarget
        end
    end
end)
-- TODO: When you set 'placing = true', you probably want to do "previousTarget = nil", too (if not, be sure that 'previousTarget' is set to nil somewhere)

If you need to access previousTarget outside this script, you'll need to turn it into an ObjectValue just like ObjTarget.

3 comments

0

Creating a objValue is generally a poor solution. You could use module scripts or something, for example. And the asker didn't say anything about needing to access previous target outside of the main script.

0

Thank you for this, I made a few changes before I got it to work. I have it so it updates the previoursTarget before target before the actually objTarget. I also made it check if there is not previous target in the same if that detects if the currentTarget does not equal the previousTarget. In the end, it managed to work out just the way I want.

0

@CootKitty: Creating an ObjectValue is a perfect solution for those who aren't experienced with ModuleScripts or need to cross the server/client boundary without using RemoteEvents/Functions.

This discussion is preserved as it was. New answers, comments, and votes are closed.