First for loop gets all the tools that has the tag “PlacementTool” but when I equipp a tool the “Equipped” runs more than twice, DB/Debounce is not preventing it.
function Update_CollectionService()
for i, PlacementTool in pairs(CollectionService:GetTagged("PlacementTool")) do -- gets all tools tagged with "PlacementObject"
local Mouse_Moving = nil
PlacementTool.Equipped:Connect(function() -- once tool Equipped
if not DB then DB = true
if not PlayerIn_EditMode then -- if tool equipped and player not in edit mode
Unequipped_Tools("Player Not In Edit Mode")
return
end
if Placement_Hologram == nil then -- if not already placing hologram
-- Clones the model in replicatedstorage to make a hologram of it to show where its being placed
Placement_Hologram = PlacableObjects:FindFirstChild(PlacementTool.Name):Clone()
print(Placement_Hologram)
-- For loop welds parts inside the hologram and also does other adjustable stuff
Weld_Hologram(Placement_Hologram)
Placement_Hologram.Parent = TempHologram -- Parents the cloned hologram to folder
-- Display Size to Info
local Size = Placement_Hologram:GetBoundingBox()
local FormatedSize_X, FormatedSize_Y, FormatedSize_Z = string.format("%.1f", Size.X), string.format("%.1f", Size.Y), string.format("%.1f", Size.Z)
Info.SizeText.Text = "Size: " .. FormatedSize_X .. ", " .. FormatedSize_Y .. ", " .. FormatedSize_Z
local function CalculateMass(Hologram) -- calculates mass by getting descendants mass and doing addition and also displays it on "Info"
Info.MassText.Text = "Mass: " .. "Calculating"
local Total_Mass = 0
for index, Part in pairs(Hologram:GetDescendants() ) do
if Part:IsA("BasePart") then
Total_Mass += Part.Mass
end
end
local Formatted_Mass = string.format("%.2f", Total_Mass)
Info.MassText.Text = "Mass: " .. Formatted_Mass.."kg"
end
CalculateMass(Placement_Hologram)
else -- no "DB = false" since this script runs more than once this statement will be ment and cause bug with the placing Hologram
print("Already Placing")
end
--- Moves hologram right to mouse once equipped rather than waiting until mouse moves
Hologram_PosCalc()
Mouse_Moving = Mouse.Move:Connect(function()
Hologram_PosCalc()
end)
--- Hologram Rotation
UserInputService.InputBegan:Connect(function(Input) -- if input began
if Input.KeyCode == Enum.KeyCode.R or Enum.KeyCode.T then
if not Rotating then Rotating = true
if Placement_Hologram and Placement_Hologram.PrimaryPart then
local orientation = Placement_Hologram.PrimaryPart.Orientation
if Input.KeyCode == Enum.KeyCode.R then -- if input is R
-- Rotates hologram on Y axis
Placement_Hologram:SetPrimaryPartCFrame(Placement_Hologram.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Degrees_Value.Value),0))
elseif Input.KeyCode == Enum.KeyCode.T then -- if input is T
-- Rotates hologram on Z axis
Placement_Hologram:SetPrimaryPartCFrame(Placement_Hologram.PrimaryPart.CFrame * CFrame.Angles(0,0, math.rad(Degrees_Value.Value)))
end
-- Updates UI info about rotation
local ori = Placement_Hologram.PrimaryPart.Orientation
Info.Ori.Text = "Orientation: " .. ori.X .. ", " .. ori.Y .. ", " .. ori.Z
end
wait() -- wait time before next rotation
Rotating = false
end
end
end)
--- Place function
PlacementTool.Activated:Connect(function() -- if clicked
local result, errorMsg = pcall(function()
PlacePlacement:FireServer(Placement_Hologram.Name, Placement_Hologram.PrimaryPart.CFrame)
end)
if not result then
warn("Couldn't Place:", PlacementTool.Name, "ErrorMsg:", errorMsg)
else
if Mouse_Moving then
Mouse_Moving:Disconnect()
end
PlacementTool:Destroy()
ResetINFO_UI()
wait()
Placement_Hologram = nil
end
end)
DB = false
end
end)
--- Unequipped function
PlacementTool.Unequipped:Connect(function()
if Mouse_Moving then
Mouse_Moving:Disconnect()
end
local HologramToDelete = TempHologram:FindFirstChild(PlacementTool.Name)
if HologramToDelete then
HologramToDelete:Destroy()
ResetINFO_UI()
wait()
Placement_Hologram = nil
end
end)
end
end