How do I make a part spawn at my mouse cursor when I click? And how do I make it despawn?
how to make a part spawn at your mouse cursor when you click?
1 answer
When asking a question please provide previous attempts that you have tried so you don't ask a "Make me this" type of question, anyways, since this is a simple question I'll answer it.
What you'll need are 3 things: A RemoteEvent, a LocalScript, and a ServerScript
First of all, let's make a RemoteEvent in ReplicatedStorage and name it GeneratePart
Secondly, let's have our LocalScript, which I had placed in StarterPlayerScripts
This script will fire the RemoteEvent so that way other players will be able to see the parts being generated, you should have this inside of it:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RP = game:GetService("ReplicatedStorage")
local Event = RP:WaitForChild("GeneratePart")
mouse.Button1Down:connect(function()
Event:FireServer(mouse.Hit.p)
end)
Finally, put the ServerScript in ServerScriptService, which will respond to the Event being fired, and put in the following:
local RP = game:GetService("ReplicatedStorage")
local Event = RP:WaitForChild("GeneratePart")
local DespawnTime = 1
Event.OnServerEvent:connect(function(player, Pos)
local Part = Instance.new("Part",workspace)
Part.Position = Pos
wait(DespawnTime)
Part:Destroy()
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
If you could help me for future reference what does (mouse.H.p) mean in line 8 after you fire your event? Does it get the Mouses Hit Position?