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

Why is it detecting my mouse is up even though its down?

Im making a destroying script, and when ever you hold down the mouse it will count untill it breaks. It keeps detecting its up even though its down.

Help?

Code:

--[[
EXTRA INFORMATION:
Parent = game.StarterPack
ScriptType = LocalScript
]]
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local mouseDistance = 30

local distance = nil

local function GetMaxDistance()
	distance = (player.Character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
	-- print(distance)
end

function DestroyBlock()
	GetMaxDistance()
	if mouse.Target.Name == "log" and distance <= mouseDistance then
		local logStregth = 5
		local setBlock = mouse.Target
		print(setBlock)
		if mouse.Button2Down then
			for i = 0,logStregth,1 do
				print(i)
				if mouse.Button2Up then
					print("Mouse went up")
					break
				end
				if i == logStregth then
					setBlock:Destroy()
				end
				wait(1)
			end
		end
	end
end
function PlaceBlock()
	GetMaxDistance()
	if distance <= mouseDistance then
		local newBlock = game.ReplicatedStorage.Assets.grass:Clone()
		newBlock.CFrame = CFrame.new(mouse.Target.Position + Vector3.new(0,4,0))
		newBlock.Parent = mouse.Target.Parent
	end
end

mouse.Button1Down:Connect(PlaceBlock)
mouse.Button2Down:Connect(DestroyBlock)

1 answer

I see your problem here. mouse.Button1Down only fires once when the mouse goes down and doesn't continue sending a message to that function to fire.

To fix, you are going to have to use mouse.Button1Up to change a variable to let the function/script know when it's down, and when it's not.

Code sample:

local Mouse1Down = false
local Mouse2Down = fasle

mouse.Button1Down:Connect(function()
	Mouse1Down = true
end)

mouse.Button2Down:Connect(function()
	Mouse2Down = true
end)

mouse.Button1Up:Connect(function()
	Mouse1Down = false
end)

mouse.Button2Up:Connect(function()
	Mouse2Down = false
end)

while wait() do -- Or other constant loop
	if Mouse1Down == true then
	-- Code
	end

	if Mouse2Down == true then
	-- Code
	end
end

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