Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

this is not working i am prob doing something stupid and wrong please help?

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)
	

2 comments

0

Describe what exactly doesn't work and what's the expected result

0

when you click it should make a ray from you camira to your where your mouse hit then clone a model from server storage and place it in workspace where the ray ends

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.