I kinda have been messing with this trying to get this to work, I think i might be on the right track
local Camera = game.Workspace.CurrentCamera local Player = game:GetService("Players").LocalPlayer local Mouse = Player:GetMouse() local Tool = script.Parent local MakeWall = script.Parent local Wall = game.ServerStorage.Model MakeWall.MouseButton1Click:connect(function() wait() local ray = Ray.new(Camera.CFrame.p, (Mouse.Hit.p - Camera.CFrame.p).unit * 300) local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true) local WallClone = Wall:Clone() WallClone.Parent = game.Workspace WallClone:SetPrimaryPartCFrame(WallClone.CFramePart.CFrame) local distance = (Camera.CFrame.p - position).magnitude local thing = ray.Origin + distance * ray.Direction.Unit WallClone.CFrame = CFrame.new(math.floor(thing.x/2 + .5)*2, math.floor(thing.y/2 + .5)*2, math.floor(thing.z/2 + .5)*2) end)
There are a couple of issues I've identified that should get your tool working.
First off, you're attempting the fire the event MouseButton1Click
from a tool, which is immediately going to throw an exception because it is not a valid event of tools. The solution to this is to set up a variable that will allow us to identify when the tool is equipped, so it can be checked whenever the player clicks the mouse.
This can be done in several simple ways, one of which may be something like:
local equipped = false MakeWall.Equipped:connect(function() equipped = true end) MakeWall.Unequipped:connect(function() equipped = false end)
The Button1Down
event can be used for us to know when the player clicks their mouse, following which we will need to immediately test if the tool is equipped:
Mouse.Button1Down:connect(function() if equipped then -- Run code end end)
There is also a problem near the end of your function, too. Namely, you are trying to set the CFrame of a Model ('WallClone'), which is going to throw an exception. As you know, you'll need to instead use SetPrimaryPartCFrame
.
Here's the fixed code that worked fine when I tested. Let me know if you hit any more problems.
local Camera = game.Workspace.CurrentCamera local Player = game:GetService("Players").LocalPlayer local Mouse = Player:GetMouse() local Tool = script.Parent local MakeWall = script.Parent local Wall = game.ServerStorage.Model local equipped = false Mouse.Button1Down:connect(function() if equipped then wait() local ray = Ray.new(Camera.CFrame.p, (Mouse.Hit.p - Camera.CFrame.p).unit * 300) local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true) local WallClone = Wall:Clone() WallClone.Parent = game.Workspace WallClone:SetPrimaryPartCFrame(WallClone.CFramePart.CFrame) local distance = (Camera.CFrame.p - position).magnitude local thing = ray.Origin + distance * ray.Direction.Unit WallClone:SetPrimaryPartCFrame(CFrame.new(math.floor(thing.x/2 + .5)*2, math.floor(thing.y/2 + .5)*2, math.floor(thing.z/2 + .5)*2)) end end) MakeWall.Equipped:connect(function() equipped = true end) MakeWall.Unequipped:connect(function() equipped = false end)