Today I attempted to create a simple placement script where the player can place stuff (not added yet) but ran into some troubles. I was going to use the Touched event but created some issues so instead I'm using Region3. The selected part is being validated can "CanMove" is being set to true if it's inside the Region3 but adding an else statement doesn't seem to do anything. I even tried printing something and nothing happened.
A concept or an explaination is all I need. Thanks.
Below is my full script which I am using.
repeat wait() until game.Players.LocalPlayer ~= nil
repeat wait() until game.Players.LocalPlayer.Character ~= nil
repeat wait() until game:IsLoaded()
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlayerGui = Player:WaitForChild("PlayerGui")
local Mouse = Player:GetMouse()
Selected = nil
CanMove = false
local Barrier = game.Workspace.Barrier
local BarrierRegion = Region3.new(Vector3.new(0,0,0), Barrier.Size)
Barrier.CFrame = BarrierRegion.CFrame
wait(2)
Selected = workspace.bRICK -- Oops, caps.
CanMove = true
function move()
if CanMove == true and Character.Humanoid.Health > 0 and Character ~= nil and Player ~= nil and Selected ~= nil then
Selected.Position = Vector3.new(math.floor(Mouse.Hit.p.X), 0, math.floor(Mouse.Hit.p.Z))
end
end
function color()
if CanMove == true and Selected ~= nil then
Selected.Color = Color3.fromRGB(255, 89, 89)
elseif CanMove ~= true then
Selected.Color = Color3.fromRGB(25,25,25)
end
end
while wait() do
color()
local parts = workspace:FindPartsInRegion3WithWhiteList(BarrierRegion, {Selected}, 1000)
for i,v in pairs(parts) do
if v == Selected then
CanMove = true
else
CanMove = false
print("Testing")
-- Testing nor disabled CanMove works.
end
end
if Selected ~= nil and Mouse.Target ~= nil and Mouse ~= nil and CanMove == true then
move()
end
end
