So, I’m making a welding parts together tool kind of thing for my game, and my script doesn’t work. There isn’t any labeled errors or such, it just doesn’t work. This is my (local) script
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local function weldParts(part1, part2)
local weld = Instance.new("WeldConstraint", part1)
weld.Part0 = part1
weld.Part1 = part2
end
local function onActivated()
local mouse = game.Players.LocalPlayer:GetMouse()
local part1 = mouse.Target
if not part1 then
return
end
wait()
local part2 = mouse.Target
if not part2 or part1 == part2 then
return
end
weldParts(part1, part2)
end
tool.Activated:Connect(onActivated)
It seems that the script is not able to detect the parts correctly using the mouse.Target. To fix this issue, you can use the ClickDetector instead. Here’s the modified version of the script using ClickDetector:
First, add a ClickDetector to your tool’s handle:
local clickDetector = Instance.new("ClickDetector", handle)
Next, modify the onActivated() function to use the ClickDetector’s MouseButton1Click event:
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
-- Add a ClickDetector to the handle
local clickDetector = Instance.new("ClickDetector", handle)
local function weldParts(part1, part2)
local weld = Instance.new("WeldConstraint", part1)
weld.Part0 = part1
weld.Part1 = part2
end
local part1, part2
local function onMouseClick(target)
if not part1 then
part1 = target
else
part2 = target
if part1 ~= part2 then
weldParts(part1, part2)
end
part1, part2 = nil, nil
end
end
clickDetector.MouseButton1Click:Connect(onMouseClick)
This script adds a ClickDetector to the tool’s handle, and it will listen for the MouseButton1Click event to detect the parts you want to weld. It will wait for two consecutive clicks and then weld the two selected parts together.
Please note that it’s a good practice to check if the clicked part is valid (e.g., not workspace, not a player character) before proceeding with the welding process. You can add those checks in the onMouseClick function.