How can I fix my placement module?

Hello, I have been working on a placement system but I have a problem, I want to make the item that the player is placing to turn red when its touching another part that’s not the grid or when it’s out of the grid else I want it to turn white here is how the module looks like ingame:


Here is the module:

local module = {}

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService('RunService')
local place
local isPlacing = false
local modelPlacing

function module:Start(i)
	local item = i:Clone()
	item.Parent = workspace
	item.CanCollide = false
	item.Transparency = .5
	modelPlacing = item
	isPlacing = true
	place = runService.RenderStepped:Connect(function()
		item.CFrame = CFrame.new(mouse.Hit.X/1.1,item.Size.Y/2 + workspace.Grid.Size.Y,mouse.Hit.Z/1.1)
	end)
end

function module:Place(item)
	place:Disconnect()
	local clonedItem = item:Clone()
	clonedItem.Parent = workspace
	clonedItem.CFrame = item.CFrame
	clonedItem.Transparency = 0
	item:Destroy()
	modelPlacing = nil
end

function module:Rotate(item)
	
end

mouse.Button1Down:Connect(function()
	if isPlacing == true then
		module:Place(modelPlacing)
		isPlacing = false
	end
end)

return module

Thank you!

1 Like

EDIT: I tried using touched but it’s very buggy.

For detecting if it’s touching another part you can use :GetTouchingParts or Region3, either would work. For detecting if something is outside the grid there’s a lot of methods you could do but the simplest would be firing a ray and seeing if whatever the ray hits is outside of the grid or not.

1 Like

How can I check what touching parts are beeing touched?

If you are wanting to use :GetTouchingParts then simply use it on a part that acts as a hitbox and it will return a table of parts.

local touchingParts = Hitbox:GetTouchingParts()

If you want to use Region3 it would be similar, just by using Region3 and using the :FindPartsInRegion3 method.

There’s multiple modules for Region3 out there such as part to region3 or rotated region3, you can find either of those with a quick google search.

2 Likes

Do I need to put this in a while loop or it will update automaticly?

local touchingParts = Hitbox:GetTouchingParts()

Thank you!

Either you would need to do it in a loop or do it whenever the object is moved if you want it to update constantly, if you don’t want it to update constantly the have it check whenever its attempted to be placed.

1 Like

Like SquidyCakez said you can use Instance:GetTouchingParts() and iterate:

for index, part in pairs(Instance:GetTouchingParts()) do
    if part.Name == "[Name]" then
        --Do what you want, like block placing, change color, etc
    end
end
1 Like

It does not work for some reason here is my code:

local module = {}

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService('RunService')
local place
local isPlacing = false
local modelPlacing

function module:Start(i)
	local item = i:Clone()
	item.Parent = workspace
	item.CanCollide = false
	item.Transparency = .5
	modelPlacing = item
	isPlacing = true
	place = runService.RenderStepped:Connect(function()
		for index, part in pairs(item:GetTouchingParts()) do
			if part.Name ~= "Grid" then
				item.Color = Color3.fromRGB(255, 0, 0)
			end
		end
		item.CFrame = CFrame.new(mouse.Hit.X/1.2,item.Size.Y/2 + workspace.Grid.Size.Y,mouse.Hit.Z/1.2)
	end)
end

function module:Place(item)
	place:Disconnect()
	local clonedItem = item:Clone()
	clonedItem.Parent = workspace
	clonedItem.CFrame = item.CFrame
	clonedItem.Transparency = 0
	item:Destroy()
	modelPlacing = nil
end

function module:Rotate(item)
	
end

mouse.Button1Down:Connect(function()
	if isPlacing == true then
		module:Place(modelPlacing)
		isPlacing = false
	end
end)

return module

EDIT I tried this too:

local module = {}

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService('RunService')
local place
local isPlacing = false
local modelPlacing

function module:Start(i)
	local item = i:Clone()
	item.Parent = workspace
	item.CanCollide = false
	item.Transparency = .5
	modelPlacing = item
	isPlacing = true
	place = runService.RenderStepped:Connect(function()
		for index, part in pairs(item:GetTouchingParts()) do
			if part.Name ~= "Grid" then
				item.Color = Color3.fromRGB(255, 0, 0)
			else
				item.Color = Color3.fromRGB(255, 255, 255)
			end
		end
		item.CFrame = CFrame.new(mouse.Hit.X/1.2,item.Size.Y/2 + workspace.Grid.Size.Y,mouse.Hit.Z/1.2)
	end)
end

function module:Place(item)
	place:Disconnect()
	local clonedItem = item:Clone()
	clonedItem.Parent = workspace
	clonedItem.CFrame = item.CFrame
	clonedItem.Transparency = 0
	item:Destroy()
	modelPlacing = nil
end

function module:Rotate(item)
	
end

mouse.Button1Down:Connect(function()
	if isPlacing == true then
		module:Place(modelPlacing)
		isPlacing = false
	end
end)

return module
```

How do I use Region3 for this? I have never tried using it sorry.