I made a building tool, using which I can place and rotate a block. I have came to an issue though, unequipping the tool does not disable the script inside it, and while tool is still in the inventory of player, I still can place and rotate blocks.
It seems like the solution for that would be connecting and disconnecting functions when tool is equipped/unequipped, but I am not really familiar with connections.
Here is the script:
local ContextActionService = game:GetService("ContextActionService")
local players = game.Players
local player = players.LocalPlayer
local tool = script.Parent
local block = tool.Block
local holo = tool.BlockHolo
local mouse = player:GetMouse()
mouse.Move:Connect(function() -- Display
local mousePosition = Vector3.new(math.round(mouse.Hit.Position.X), math.round(mouse.Hit.Position.Y), math.round(mouse.Hit.Position.Z))
local target = mouse.Target
local targetSurface = mouse.TargetSurface
if targetSurface == Enum.NormalId.Top then
holo.Position = mousePosition + target.CFrame.UpVector
elseif targetSurface == Enum.NormalId.Bottom then
holo.Position = mousePosition - target.CFrame.UpVector
elseif targetSurface == Enum.NormalId.Front then
holo.Position = mousePosition + target.CFrame.LookVector
elseif targetSurface == Enum.NormalId.Back then
holo.Position = mousePosition - target.CFrame.LookVector
elseif targetSurface == Enum.NormalId.Right then
holo.Position = mousePosition + target.CFrame.RightVector
elseif targetSurface == Enum.NormalId.Left then
holo.Position = mousePosition - target.CFrame.RightVector
end
end)
local function RotateAndPlace(actionName, inputState, inputObject) -- Rotate
if inputObject.KeyCode == Enum.KeyCode.R and inputState == Enum.UserInputState.Begin then
holo.CFrame = holo.CFrame * CFrame.Angles(0, math.rad(90), 0)
elseif inputObject.KeyCode == Enum.KeyCode.T and inputState == Enum.UserInputState.Begin then
holo.CFrame = holo.CFrame * CFrame.Angles(0, 0, math.rad(90))
elseif inputObject.UserInputType == Enum.UserInputType.MouseButton1 and inputState == Enum.UserInputState.Begin then
local clone = holo:Clone()
clone.Transparency = 0
clone.Parent = workspace
clone.Name = "Block"
clone.CanCollide = true
clone.Anchored = false
local weld = Instance.new("WeldConstraint")
weld.Parent = clone
weld.Part0 = clone
weld.Part1 = mouse.Target
end
end
ContextActionService:BindAction("Rotate and Place", RotateAndPlace, false, Enum.KeyCode.R, Enum.KeyCode.T, Enum.UserInputType.MouseButton1)
I really need help with that. It would be quite nice if somebody explained how to Disconnect()
functions properly.