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.
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)
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.
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.
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?
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?
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
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”.
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.