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
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.
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.
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
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
```