Placement System Glitch

I was making a placement system in studio for fun, however when I enabled the build mode the placement would act weird. Then, when I disabled and reenabled it, it worked just fine.

Upon First Enable:

Upon Disable and 2nd Activation:

Script:

local plrs, rep = game.Players, game.ReplicatedStorage
local plr = plrs.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local run, cs = game:GetService("RunService"), game:GetService("CollectionService")
local uis, mouse = game:GetService("UserInputService"), plr:GetMouse()
mouse.TargetFilter = workspace.Camera

local buildModeOn = false
local currentPart = nil
print("Initiating")

local function updatePart(pos : Vector3, oldPart : Instance)
	print("Updating!!!")
	if oldPart then oldPart:Destroy() end
	local newPart = Instance.new("Part", workspace)
	newPart.Color = Color3.fromRGB(0,255,0)
	newPart.Position = pos + Vector3.new(0, newPart.Size.Y / 2, 0)
	newPart.Transparency = 0.7
	newPart.Anchored = true
	newPart.CanCollide = false
	return newPart
end

uis.InputBegan:Connect(function(input : InputObject)
	if input.KeyCode == Enum.KeyCode.E then
		if buildModeOn == false then
			buildModeOn = true
			print("Build mode on")
			local temp = nil
			temp = updatePart(Vector3.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z))
			currentPart = temp
			temp = nil
			mouse.Move:Connect(function()
				if not buildModeOn then return end
				temp = nil
				temp = updatePart(mouse.Hit.Position, currentPart)
				currentPart = temp 
				temp = nil
			end)
		else
			buildModeOn = false
			print("Build mode off")
			currentPart:Destroy()
		end
	end
end)

If the script looks kind of messy, well my plan is that if this all works out and I don’t get lazy I’ll make it modular (this is for fun so please dont spam ads at me)

1 Like

I don’t know why it works at all. mouse.TargetFilter should be set to the part?

2 Likes

I put mouse.TargetFilter = newPart at the end of updatePart function before the return and it worked!

What I’m thinking is that when I was moving my mouse is small increments the position would be calculated on top of the existing part which caused the new part to go on top and so on…

Either way it’s fixed, thank you!

1 Like

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