Hi, i try to make a model placing script. I kinda managed that the model is following the mouse when the mouse is targeting the user base. But when i click instead of spawning the model once it spams itlike 20-30 times. Shouldnt the debounce prevent that?

local rs = game:GetService("ReplicatedStorage")

local b = rs:WaitForChild("Build")

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local debounce = false

function b.OnClientInvoke()
local model = rs.Items.Field:Clone()
	model.Parent = workspace
	
	local plot = workspace.Plot1
	
	local x = plot:GetModelCFrame().p.X
	local y = plot:GetModelCFrame().p.Y
	local z = plot:GetModelCFrame().p.Z
	
	local px = 50
	local py = 0.2
	local pz = 50
	
	local fx = 5.4
	local fy = model.Base.Size.Y
	local fz = 5.4
	
	minx = -20
	maxx = 20
	
	minz = -20
	maxz = 20

	
	local mx,my,mz, maxmx,maxmz,minmx,minmz
	
	mouse.Move:Connect(function()
		if debounce == false then
			debounce = true
			
			if mouse.Hit.X >= 0 then
				mx = math.floor(mouse.Hit.X) - x
			else
				mx = x - math.floor(mouse.Hit.X)
			end
			
			if mouse.Hit.Z >= 0 then
				mz = math.floor(mouse.Hit.Z) - z
			else
				mz = z - math.floor(mouse.Hit.Z)
			end
			
			maxmx = mx + 2.7
			minmx = mx - 2.7
			
			maxmz = mz + 2.7
			minmz = mz - 2.7
			
			if maxmx <= maxx and minmx >= minx then
				if maxmz <= maxz and minmz >= minz then
					model:MoveTo(Vector3.new(math.floor(mouse.Hit.X),y,math.floor(mouse.Hit.Z)))
					
				end
			else
				model:MoveTo(Vector3.new(0,0,0))
			end
	
		
		mouse.Button1Down:Connect(function()

					local model = model:Clone()
					model.Base.CanCollide = true
					model.Part.CanCollide = true
					model.Parent = workspace.Plot1
					wait(0.5)
					model:MoveTo(Vector3.new(math.floor(mouse.Hit.X),y,math.floor(mouse.Hit.Z)))

			
		end)
		wait(0.1)
		debounce = false
		end
	end)
end

1 comment

0

You have a debounce for on mouse move event but you don't have one for your mouse button one down event. They're separate listeners when connected to the RbxSignalConnection