I asked a question a few days ago asking how to detect collision with a placement system. A user responded saying that region3 would work. Using the advice I am trying to make a collision system using region3 but nothing has worked so far. Anytime I move my mouse it gives an error and when I place the model it gives an error. I am new to region3's so if I am doing something completely wrong then let me know. I have not started to get collision working because the region3 is not even starting to work. Anyway heres my code:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local PosX
local PosY
local PosZ
local Model
local Toggle = nil
local Gui = script.Parent
local Button = Gui.TextButton
local Button2 = Gui.Model1
local IsPlacing = nil
local GridId = "rbxassetid://13786085"
local UserInputService = game:GetService("UserInputService")
local currentRot = 0
local CanStart = true
local SelectionBox = script.SelectionBox
local Hit = script.Hitboxs
local IsRotating = nil
local function SnapToGrid()
local Snap = 2
local Grid = 2
local RoundVariable = 0.5
if Model:IsA("Model") then
PosY = Model.PrimaryPart.Position.Y
Grid = 2
Snap = 2
elseif Model:IsA("Part") then
PosY = Model.Position.Y
Grid = 2
Snap = 2
end
PosX = math.floor(Mouse.Hit.X / Snap + RoundVariable) * Grid
PosZ = math.floor(Mouse.Hit.Z / Snap + RoundVariable) * Grid
end
local function Placement()
if Toggle == true and Model:IsA("Part") and Mouse.Target and Mouse.Target.Name == "Baseplate" then
Gui.Rotate.Visible = false
Gui.Cancel.Visible = false
local PlacedModel = Model:Clone()
PlacedModel.Parent = workspace.Clones
Model:Destroy()
Toggle = nil
IsPlacing = nil
game.Workspace.Baseplate.Texture.Texture = ""
PlacedModel.CanCollide = true
PlacedModel.Position = Vector3.new(PosX, PosY, PosZ)
CanStart = true
currentRot = 0
SelectionBox.Transparency = 1
SelectionBox.SurfaceTransparency = 1
elseif Toggle == true and Model:IsA("Model") then
Gui.Rotate.Visible = false
Gui.Cancel.Visible = false
local PlacedModel = Model:Clone()
PlacedModel.Parent = workspace.Clones
Model:Destroy()
Toggle = nil
IsPlacing = nil
local Pos1, Pos2 = (PlacedModel.PrimaryPart.Position - PlacedModel.PrimaryPart.Size / 2) (PlacedModel.PrimaryPart.Position + PlacedModel.PrimaryPart.Size / 2)
RegionModel = Region3.new(
Pos1,
Pos2
)
game.Workspace.Baseplate.Texture.Texture = ""
local ModelProperties = PlacedModel:GetChildren()
currentRot = 0
SelectionBox.Transparency = 1
SelectionBox.SurfaceTransparency = 1
for _, Children in pairs(ModelProperties) do
Children.CanCollide = true
end
CanStart = true
end
end
local function Movement()
if IsPlacing == true and Model:IsA("Model") and Mouse.Target and Mouse.Target.Name == "Baseplate" then
Mouse.TargetFilter = Model
SnapToGrid()
Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ) * CFrame.Angles(0, math.rad(currentRot), 0))
Toggle = true
local ModelTable = Model:GetChildren()
for _, Children in pairs(ModelTable) do
Children.CanCollide = false
Children.Anchored = true
end
local Region = workspace:FindPartsInRegion3(RegionModel, workspace.Baseplate, 100)
local PartsInRegion = {}
if IsRotating ~= true then
SelectionBox.Color3 = Color3.fromRGB(0, 255, 3)
SelectionBox.SurfaceColor3 = Color3.fromRGB(95, 215, 75)
end
elseif IsPlacing == true and Model:IsA("Part") and Mouse.Target and Mouse.Target.Name == "Baseplate" then
Mouse.TargetFilter = Model
SnapToGrid()
Model.Position = Vector3.new(PosX, PosY, PosZ)
Toggle = true
Model.CanCollide = false
Model.Anchored = true
if IsRotating ~= true then
SelectionBox.Color3 = Color3.new(0, 1, 0)
SelectionBox.SurfaceColor3 = Color3.new(0, 0.5, 0)
end
elseif Mouse.Target or Mouse.Target == nil and not Mouse.Target.Name == "Baseplate" and IsPlacing then
SelectionBox.Color3 = Color3.fromRGB(255, 47, 85)
SelectionBox.SurfaceColor3 = Color3.fromRGB(255, 40, 80)
Toggle = false
else
return Mouse.Hit.p
end
end
local function StartPlacement1()
if Player.leaderstats.Cash.Value >= 50 and CanStart then
game.Workspace.Baseplate.Texture.Texture = GridId
Model = game.ReplicatedStorage.Movement:Clone()
Model.Parent = workspace
Mouse.Move:Connect(Movement)
IsPlacing = true
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - 50
CanStart = false
Gui.Rotate.Visible = true
Gui.Cancel.Visible = true
SelectionBox.Adornee = Model
SelectionBox.Transparency = 0.2
SelectionBox.SurfaceTransparency = 0.8
end
end
local function StartPlacement2()
if Player.leaderstats.Cash.Value >= 100 and CanStart then
game.Workspace.Baseplate.Texture.Texture = GridId
Model = game.ReplicatedStorage.Tree:Clone()
Model.Parent = workspace
Mouse.Move:Connect(Movement)
IsPlacing = true
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - 100
CanStart = false
Gui.Rotate.Visible = true
Gui.Cancel.Visible = true
SelectionBox.Adornee = Model
SelectionBox.Transparency = 0.2
SelectionBox.SurfaceTransparency = 0.8
end
end
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.R and IsPlacing and Model:IsA("Model") and not gameProcessedEvent then
for i = 1, 18, 1 do
currentRot = currentRot + 5
wait(0,0.1)
Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ) * CFrame.Angles(0, math.rad(currentRot), 0))
SelectionBox.Color3 = Color3.fromRGB(255, 47, 85)
SelectionBox.SurfaceColor3 = Color3.fromRGB(255, 40, 80)
Toggle = false
IsRotating = true
end
SelectionBox.Color3 = Color3.new(0, 1, 0)
SelectionBox.SurfaceColor3 = Color3.new(0, 0.5, 0)
Toggle = true
IsRotating = false
elseif inputObject.KeyCode == Enum.KeyCode.R and IsPlacing and Model:IsA("Part") and not gameProcessedEvent then
for i = 1, 9, 1 do
currentRot = currentRot + 10
wait(0,1)
SelectionBox.Color3 = Color3.fromRGB(255, 47, 85)
SelectionBox.SurfaceColor3 = Color3.fromRGB(255, 40, 80)
Toggle = false
Model.Orientation = Vector3.new(0, currentRot, 0)
IsRotating = true
end
SelectionBox.Color3 = Color3.new(0, 1, 0)
SelectionBox.SurfaceColor3 = Color3.new(0, 0.5, 0)
Toggle = true
IsRotating = false
elseif inputObject.KeyCode == Enum.KeyCode.C then
local ClonesTable = workspace.Clones:GetChildren()
for i, v in pairs(ClonesTable) do
if v then
v:Destroy()
end
end
else
return currentRot
end
end
function StopPlacement(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.X and IsPlacing and not gameProcessedEvent then
SelectionBox.Parent = script
SelectionBox.Transparency = 1
SelectionBox.SurfaceTransparency = 1
IsPlacing = false
Model:Destroy()
game.Workspace.Baseplate.Texture.Texture = ""
CanStart = true
currentRot = 0
Toggle = false
Gui.Rotate.Visible = false
Gui.Cancel.Visible = false
end
end
UserInputService.InputBegan:Connect(onKeyPress)
UserInputService.InputBegan:Connect(StopPlacement)
Button.MouseButton1Click:Connect(StartPlacement1)
Button2.MouseButton1Click:Connect(StartPlacement2)
Mouse.Button1Down:Connect(Placement)
Thanks for the help!
