Why won't this spawn a part where my mouse clicks?

Hey! So I am trying to make a tool where when you have it equipped it will spawn a part where you click, I am wanting to do this but the code I have made below will not work?

Does anyone know why?

local Mouse = script.Parent.Parent.Parent:GetMouse()
	
script.Parent.Equipped:Connect(function(click)
	click.Button1Down:Connect(function()
		local Part = Instance.new("Part",game.Workspace);
		Part.Shape = "Ball"
		Part.Size = Vector3.fromAxis(0.5, 0.5, 0.5)
		Part.Material = "Neon"
		Part.Color = Color3.fromRGB(255,255,255)
		Part.Name = "WeldPart"
		Part.CFrame = CFrame.new(Mouse.Target)
	end)
end)

Psst,

This is bad practice to use the Parent argument anywho:


FYI: click is the PlayerMouse, no need to use GetMouse. Also, Vector3.fromAxis uses a Axis Enumeration - did you mean to use Vector3.new?

2 Likes

Haha thanks I have changed it :smiley:

1 Like

I’m dumb, my brain automatically ignores the parent argument of Instance.new…

Mouse.Target is a part the mouse is pointing at. Use Mouse.Hit instead.
Part.CFrame = Mouse.Hit

As an additional note, check the output window for errors. Most issues here don’t require help.

2 Likes

Okay, thanks this is my code now, but I still don’t know why the part won’t spawn where the mouse clicks?

local Mouse = script.Parent.Parent.Parent:GetMouse()
	
script.Parent.Equipped:Connect(function(click)
	click.Button1Down:Connect(function()
		local Part = Instance.new("Part")
		Part.Shape = "Ball"
		Part.Parent = workspace
		Part.Size = Vector3.fromAxis(0.5, 0.5, 0.5)
		Part.Material = "Neon"
		Part.Color = Color3.fromRGB(255,255,255)
		Part.Name = "WeldPart"
		Part.CFrame = Mouse.Hit
	end)
end)

image

Really sorry if it’s obvious (there is nothing in the output that’s why I am confused) :slight_smile:

Does a new part even appear in the explorer? If so, what is its position?

1 Like

Nope, nothing spawns it seems like it can’t even reach that part of the script…

Try replacing this:

script.Parent.Equipped:Connect(function(click)
	click.Button1Down:Connect(function()

With this:

script.Parent.Activated:Connect(function()

Also replace the “click” with “Mouse”.

1 Like

NVM, I see the issue. The problem is in networking.

Try creating a LocalScript with the same code. The problem is that mouse can only be checked on the client’s side. You’ll have to make the LocalScript handle input (click position) and a separate normal Script for the parts. Also, use remote events to communicate.

Ah okay let me give it a try also I tried what you said before, and I got this error 12:25:26.030 - Players.TheReal4Cedar123.Backpack.Weld Tool V2.Script:13: attempt to index nil with ‘Hit’

local Mouse = script.Parent.Parent.Parent:GetMouse()
	
script.Parent.Activated:Connect(function()
	--Mouse.Button1Down:Connect(function()
		local Part = Instance.new("Part")
		Part.Shape = "Ball"
		Part.Parent = workspace
		Part.Size = Vector3.fromAxis(0.5, 0.5, 0.5)
		Part.Material = "Neon"
		Part.Color = Color3.fromRGB(255,255,255)
		Part.Name = "WeldPart"
		--Part.CFrame = CFrame.new(Mouse.Target)
		Part.CFrame = Mouse.Hit
	--end)
end)

In short:

LocalScript:

local remev = script.Parent.OnClickEvent
script.Parent.Equipped:Connect(function(click)
	click.Button1Down:Connect(function()
		remev:FireServer(click.Hit)
	end)
end)

Script:

local remev = script.Parent.OnClickEvent
remev.OnServerEvent:Connect(function(plyr,hit)
	local Part = Instance.new("Part")
	Part.Shape = "Ball"
	Part.Size = Vector3.fromAxis(0.5, 0.5, 0.5)
	Part.Material = "Neon"
	Part.Color = Color3.fromRGB(255,255,255)
	Part.Name = "WeldPart"
	Part.CFrame = hit
	Part.Parent = workspace
end)
4 Likes

Wow thanks, I tried it and did some things and I get this error 12:33:00.995 - Players.TheReal4Cedar123.Backpack.Weld Tool V2.Script:9: invalid argument #1 to ‘new’ (Vector3 expected, got CFrame) - Part.CFrame = CFrame.new(hit)

Part.CFrame = hit

Forgot to add. You’ll have to a :Disconnect() for the Button1Down after the tool is unequipped, as it will create 2 parts next time you equip it.

1 Like

Thank you so much!!! :smiley:

:smiley: :smiley: :smiley:

Hey there! This code is mostly right except for a couple things, you’re doing Mouse.Target which does not give a CFrame but instead whatever the mouse is hovering over, and what I’m presuming by local Mouse = script.Parent.Parent.Parent:GetMouse() is that this is not in a local script, in order to use mouse you need to have it in a local script, so put this in a local script and the following code.

local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
	
script.Parent.Activated:Connect(function() -- no need to do equipped since you can do Tool.Activated
	local Part = Instance.new("Part")
    Part.Parent = workspace
	Part.Shape = "Ball"
	Part.Size = Vector3.new(0.5, 0.5, 0.5) -- use .new to set the size.
	Part.Material = Enum.Material.Neon -- it is reccommended to use Enums instead of just stating the material, not sure why.
	Part.Color = Color3.fromRGB(255,255,255)
	Part.Name = "WeldPart"
	Part.CFrame = Mouse.Hit -- mouse.hit is the mouses CFrame
end)
-- note that this will only be on the clients side, in order to replicate it you'd need to use events

Preview

Edits: adding preview and fixing my sentence

1 Like