How do I make a part spawn at my mouse cursor when I click? And how do I make it despawn?
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)
Closed as Not Constructive by IAmNotTheReal_MePipe and JesseSong
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?