It's simple. CFrame property calls CFrame.new and Vector3.new over it:
tool = script.Parent
handle = tool.Handle
p = game.Players.LocalPlayer
m = p:GetMouse()
tool.Equipped:connect(function()
local p1
local p2
tool.Activated:connect(function()
if not p1 then
p1 = m.Hit.p
else
p2 = m.Hit.p
end
if p1 and p2 then
local part = Instance.new("Part", game.Workspace)
local dist = (p1-p2).magnitude
print("Distance :"..dist)
part.CFrame = CFrame.new(Vector3.new(p1.X, p2.Z))
part.Size = Vector3.new(dist,1,1)
end
end)
end)
But it does not work because once generated, the part will fall, so do not forget to anchor it.
tool = script.Parent
handle = tool.Handle
p = game.Players.LocalPlayer
m = p:GetMouse()
tool.Equipped:connect(function()
local p1
local p2
tool.Activated:connect(function()
if not p1 then
p1 = m.Hit.p
else
p2 = m.Hit.p
end
if p1 and p2 then
local part = Instance.new("Part", game.Workspace)
part.Anchored = true
local dist = (p1-p2).magnitude
print("Distance :"..dist)
part.CFrame = CFrame.new(Vector3.new(p1.X, p2.Z))
part.Size = Vector3.new(dist,1,1)
end
end)
end)
But there's another problem. With the clicks, the tool starts getting bugged. To prevent it, immediately after the part generation, we will "clear" p1 and p2 so they can be assigned with new values. Who's better than nil value?
tool = script.Parent
handle = tool.Handle
p = game.Players.LocalPlayer
m = p:GetMouse()
tool.Equipped:connect(function()
local p1
local p2
tool.Activated:connect(function()
if not p1 then
p1 = m.Hit.p
else
p2 = m.Hit.p
end
if p1 and p2 then
local part = Instance.new("Part", game.Workspace)
part.Anchored = true
local dist = (p1-p2).magnitude
print("Distance :"..dist)
part.CFrame = CFrame.new(Vector3.new(p1.X, p2.Z))
part.Size = Vector3.new(dist,1,1)
p1 = nil
p2 = nil
end
end)
end)
And a thing. When you are trying to set Z, you are setting Y value instead. To correct this, you must to set a value to Y. I recommend to use p1 or p2
tool = script.Parent
handle = tool.Handle
p = game.Players.LocalPlayer
m = p:GetMouse()
tool.Equipped:connect(function()
local p1
local p2
tool.Activated:connect(function()
if not p1 then
p1 = m.Hit.p
else
p2 = m.Hit.p
end
if p1 and p2 then
local part = Instance.new("Part", game.Workspace)
part.Anchored = true
local dist = (p1-p2).magnitude
print("Distance :"..dist)
part.CFrame = CFrame.new(Vector3.new(p1.X, p1.Y, p2.Z))
part.Size = Vector3.new(dist,1,1)
p1 = nil
p2 = nil
end
end)
end)
If you want to make the part disappear of the game after a while, I think you can do it by yourself :p. But, because I'm friend:
tool = script.Parent
handle = tool.Handle
p = game.Players.LocalPlayer
m = p:GetMouse()
tool.Equipped:connect(function()
local p1
local p2
tool.Activated:connect(function()
if not p1 then
p1 = m.Hit.p
else
p2 = m.Hit.p
end
if p1 and p2 then
local part = Instance.new("Part", game.Workspace)
part.Anchored = true
local dist = (p1-p2).magnitude
print("Distance :"..dist)
part.CFrame = CFrame.new(Vector3.new(p1.X, p1.Y, p2.Z))
part.Size = Vector3.new(dist,1,1)
p1 = nil
p2 = nil
wait(3)
part.Parent = nil
end
end)
end)
1 comment
Distance :22.704313278198 13:11:57.696 - Players.Player1.Backpack.Tool.LocalScript:20: bad argument #1 to 'new' (Vector3 expected, got number) 13:11:57.697 - Script 'Players.Player1.Backpack.Tool.LocalScript', Line 20 13:11:57.697 - Stack End