Grid placing system running 3 times?

I’m making a grid placing system, which works perfectly. I have a GUI where you can select which block you want to place, each button has a value in it that points to which block is assigned to that button.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RS = game:GetService("ReplicatedStorage")

local function PlaceBlock(Block)
	print("Started")
	if #workspace.GhostBlock:GetChildren() == 1 then
		workspace.GhostBlock:GetChildren()[1]:Destroy()
	end
	--print(Block)
	Block.Anchored = true
	Block.Orientation = Vector3.new(0,0,0)

	local BlockGhost = Block:Clone()
	BlockGhost.Parent = workspace.GhostBlock
	BlockGhost.Transparency = 0.5
	BlockGhost.CanCollide = false

	Mouse.TargetFilter = BlockGhost

	local increment = 4

	Mouse.Move:Connect(function()
		local MousePos = Mouse.Hit.Position
		local BlockPos = Vector3.new(0, Block.Size.Y / 2, 0)
		BlockPos += Vector3.new(MousePos.X - Block.Size.X / 2, MousePos.Y, MousePos.Z - Block.Size.Z / 2)

		local XPos = (Block.Size.X / 2) + math.round(BlockPos.X / increment) * increment
		local YPos = math.round(BlockPos.Y)
		local ZPos = (Block.Size.Z / 2) + math.round(BlockPos.Z / increment) * increment
		BlockPos = Vector3.new(XPos, YPos, ZPos)

		BlockGhost.Position = BlockPos
		local parts = workspace:GetPartsInPart(BlockGhost)
		if #parts >= 1 then
			for i, v in pairs(parts) do
				task.spawn(function()
					if v:IsA("BasePart") then
						BlockGhost.Color = Color3.fromRGB(255,0,0)
					end
				end)
			end
		else
			BlockGhost.Color = Color3.fromRGB(0,255,0)
		end
	end)

	Mouse.Button1Down:Connect(function()
		print("Mouse down")
		local pos = BlockGhost.Position
		--print(Block.Name.." sent")
		RS.Events.PlaceBlock:FireServer(Block, pos)
	end)
end

local main = script.Parent
main.one.MouseButton1Click:Connect(function()
	local block = main.one.Value.Value
	PlaceBlock(block)
end)

main.two.MouseButton1Click:Connect(function()
	local block = main.two.Value.Value
	PlaceBlock(block)
end)

main.three.MouseButton1Click:Connect(function()
	local block = main.three.Value.Value
	PlaceBlock(block)
end)

However, it says ‘Mouse down’ 3 times when i select a second block, and also places 2 blocks, while when I ask it to print, it only says one block, and that block is the right one. What could be this issue?

1 Like

Might be because you have 3 mousebutton1click events. Not might, definitely. I’m pretty sure you can do main.one.value.value, maintwo, etc in just one mousebutton1click event. Just need to do some checks but it should work. And then I think it running 3 times will stop.

1 Like

Could you show an example if possible?

local main = script.Parent
for i, gui in main:GetChildren() do
gui.MouseButton1Click:Connect(function()
if gui.Name == "one" then
-- Do your code for it
elseif gui.Name == -- I think you get the idea now.
end)

Still runs 3 times sadly.

I’m pretty sure it has something to do with this, since it prints “Mouse down” 3 times.

Oh yeah the print statement will run 3 times because its in a for loop, but it won’t ACTUALLY run 3 times. It’ll run once for each different gui button.

1 Like

Yes, however I don’t have a print function in the loop you mentioned. I am referring to a bit higher up in the script.

Fixed it using the :Disconnect() function. For anyone wondering, I changed

Mouse.Button1Down:Connect(function()
		print("Mouse down")
		local pos = BlockGhost.Position
		--print(Block.Name.." sent")
		RS.Events.PlaceBlock:FireServer(Block, pos)
	end)

to

connection = Mouse.Button1Down:Connect(function()
					print("Mouse down")
					local pos = BlockGhost.Position
					RS.Events.PlaceBlock:FireServer(Block, pos)
					connection:Disconnect()
				end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.