this is a tool that is supposed to be used to weld 2 parts together for my game (it clicks 2 parts and then clicks again to make the weld)
i tested with some friends and the tool is clientsided, will only weld properly for the person using the tool
how do i make it not clientsided?
local Tool = script.Parent
local object1= nil
local object2= nil
local Players = game:GetService("Players")
local person = nil
Tool.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local person = Tool.Parent
if object1== nil then
local Magnitude = (Mouse.hit.Position - person.Head.Position).magnitude
if Magnitude < 20 and Mouse.Target and Mouse.Target:FindFirstChild("CanGrab") then
object1 = Mouse.Target
local hl = Instance.new("Highlight")
hl.Parent = object1
hl.OutlineColor = Color3.fromRGB(255, 128, 130)
hl.FillTransparency = 0.8
hl.FillColor = Color3.fromRGB(255, 128, 130)
end
elseif object2 == nil then
local Magnitude = (Mouse.hit.Position - person.Head.Position).magnitude
if Magnitude < 20 and Mouse.Target and Mouse.Target:FindFirstChild("CanGrab") then
object2 = Mouse.Target
local hl2 = Instance.new("Highlight")
hl2.Parent = object2
hl2.OutlineColor = Color3.fromRGB(148, 171, 255)
hl2.FillTransparency = 0.8
hl2.FillColor = Color3.fromRGB(148, 171, 255)
end
else
local weldy = Instance.new("WeldConstraint")
object1.Highlight:Destroy()
object2.Highlight:Destroy())
weldy.Part0 = object1
weldy.Part1 = object2
weldy.Parent = object1
if object1.Anchored==true and object2.Anchored==true then
object1.WeldConstraint:Destroy()
end
object1 = nil
object2 = nil
end
end)
end)
Tool.Unequipped:Connect(function(mouse)
if object1 ~= nil then
object1.Highlight:Destroy()
object1 = nil
end
if object2 ~= nil then
object2.Highlight:Destroy()
object2 = nil
end
end)```
Also, you need to be careful with events. I see you’re connecting/creating mouse button event every time the player equip the tool; this is a memory leak and can slow your game. You can test by printing something, it should only print once when you click.
You’re gonna want to move the weld creation code to a server script and use a RemoteEvent to tell the server which parts the player selected.
This code assumes the server and client scripts as well as the RemoteEvent are children of the tool.
--// CLIENT SCRIPT (or at least a shortened portion of it)
if object1 == nil then
--// Do whatever here
elseif
--// Do whatever here (the sequel)
else
--// We have our parts! Let's tell the server and clean up the highlights
script.Parent.RemoteEvent:FireServer(object1,object2)
object1.Highlight:Destroy()
object2.Highlight:Destroy())
end
--// SERVER SCRIPT
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr,object1,object2)
local Weld = Instance.new("WeldConstraint")
Weld.Part0 = object1
Weld.Part1 = object2
Weld.Parent = object1
if object1.Anchored == true and object2.Anchored == true then
--// Both parts are anchored; no need to weld them together
object1.WeldConstraint:Destroy()
end
end)