Mouse.Target error (check desc)

	mouse.Button1Down:Connect(function()
					if Target.Name ~= "Baseplate" then
						touch += 1
					end
					
					selectionBox.Adornee = Target
					if touch == 1 then
						
						print('1')
						selectionBox.Adornee = Target
						selectionBox.SurfaceColor3 = Color3.new(0.631373, 0.537255, 1)
					end
					
					if touch >= 2 then
						touch = 0
						print("2")
						
						selectionBox.Adornee = nil
					end

“Touch” is a number value, everytime the player clicks, touch goes up by one. If touch is 1, it will set the selectionbox.adornee to the mouse.Target. If it’s 2 or bigger, it will set the selectionbox.adornee to nil. Now, I set up some print functions in every if statement.

But, everytime I click the block, it prints

1  -  Client - LocalScript:31
2  -  Client - LocalScript:38
1  -  Client - LocalScript:31
2  -  Client - LocalScript:38
1  -  Client - LocalScript:31
2  -  Client - LocalScript:38
1  -  Client - LocalScript:31
1 Like

Full script if needed:

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

local selectionBox = workspace.Terrain:WaitForChild("Touch")

local touch = 0

task.spawn(function()
	while wait(.1) do
		local Target = mouse.Target
		
		if Target and Target.Name ~= "Baseplate" then
			local distance = (Target.Position - player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
			
			if distance < 35 then
				if touch ~= 1 then
					selectionBox.Adornee = Target
					
					selectionBox.Color3 = Color3.new(1, 0.415686, 0.415686)
					selectionBox.SurfaceColor3 = Color3.new(1, 0.415686, 0.415686)
				end
				
				mouse.Button1Down:Connect(function()
					if Target.Name ~= "Baseplate" then
						touch += 1
					end
					
					selectionBox.Adornee = Target
					if touch == 1 then
						
						print('1')
						selectionBox.Adornee = Target
						selectionBox.SurfaceColor3 = Color3.new(0.631373, 0.537255, 1)
					end
					
					if touch >= 2 then
						touch = 0
						print("2")
						
						selectionBox.Adornee = nil
					end
				end)
			end
		else
			if touch ~= 1 then
				selectionBox.Adornee = nil
			end
		end
	end
end)
1 Like

i didnt quite understand the “error” but it just prints 1 and 2 because the line before print("2") is touch = 0 so the number value “touch” never gets to 3 because once it gets to 2 it resets back to 0
i hope thats the issue your having.

1 Like

First of all, please tell me your intention exactly with this script, I’m quite not getting what you’re trying to achieve with this.

But first we should start by checking if the mouse.Target is nil. This is not checked in your code.

Add following to your code:

-- previous code goes here
task.spawn(function()
    while wait() do
        if mouse == nil then return end
        local Target = mouse.Target
        if Target == nil then return end
-- code continues here

With this, we check if the mouse is nil, and that before defining the target as Target is a property of mouse and it will not be definable when the Property holder is nil.
After that we check if the mouse.Target is nil. And that happens quite often (as far as I know if your cursor is on the skybox, the mouse.Target is nil.

Try it out again and if the problem persists, please reply to this explaining what you tried to do with the code and I’ll try my best to asisst you further.

1 Like

I am checking if its nil
char char char

1 Like

And you are sure you are not clicking, while your cursor is on the baseplate?

1 Like

I am sure
!!!

1 Like

all it prints is

  21:06:43.155  1  -  Client - LocalScript:31
  21:06:43.155  2  -  Client - LocalScript:37
  21:06:43.155  1  -  Client - LocalScript:31
  21:06:43.155  2  -  Client - LocalScript:37
  21:06:43.155  1  -  Client - LocalScript:31
  21:06:43.155  2  -  Client - LocalScript:37
  21:06:43.155  1  -  Client - LocalScript:31
  21:06:43.155  2  -  Client - LocalScript:37

Well what do you want to achieve. It’s simple output, which you told the script to print. Am I missing something or have you never mentioned what you want to achieve with this?

1 Like

I did mention it

chahr char hcacr hcar

1 Like

No, you described your code. You never said what happens in actual runtime. Does the selectionbox not get adorneed. do you want touch to be 3 or 4? What is it?

1 Like

I actually did mention what I want to do; heres a simple version

Everytime you HOVERS over a block (that isnt baseplate), it will select it with a selection box. Everytime you CLICK it, it will select it with a selection box until the player clicks on the block again

1 Like

Okay, dude. Please, next time tell everyone that it just prints out output and actually doesn’t adornee anything. You just said “it does this and that and my console says this”.

Is the adornee of selectionbox your problem?

1 Like

It does adornee it, buddy :skull:
characterLimitMakesMeMad = true;

1 Like

If you paid any attention to my previous posts, you’d know whhat I’m talking about.

as you can see: it does adornee it.

Please don’t tell me your issue is that 1 and 2 loop.

No. My issue is that it wont select anything + it prints “1,2,1,2,1,2”, which basically means theres something wrong

It prints “1,2,1,2,1,2” every time that I click it

So your problem is that those loop. And I thought it was intended as it was so obvious. Literally, I didn’t get it, either it is my mental mistake or my mental mistake. But I ask you politely to use the post template the next time. But now I can help you.

Well the reason is obvious. You are changing the touch variable but in the same code you check the value of your touch variable. When the code is only supposed to run once (this is what I didn’t get) then you shouldn’t change touch.
Now before writing an essay and at the end it was the wrong problem.

Is this what you are talking about?