I’m trying to find a way to only delete one part from where the player’s cursor is pointing to.
All the parts in the folder gets deleted instead.
script
local re = script.Parent:WaitForChild("RemoteEvent")
local ignorelist = {"SpawnLocation"}
local list = game.Workspace.Folder
re.OnServerEvent:Connect(function(plr)
for i, v in pairs(list:GetChildren()) do
if v.Name ~= ignorelist[1] then
v:Destroy(i)
end
end
end)
To do that, firstly you need to define what part to delete, example:
The example below detects when player press the left button of the mouse and then fires server event
Local Script in StarterPlayer > StarterPlayerScripts:
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local remoteEvent = replicatedStorage.remoteEvent
mouse.Button1Down:Connect(function() -- when the left button of the mouse is down, call the function
local target = mouse.Target -- get the mouse target
if not target then return end -- check if mouse target exists
remoteEvent:FireServer(mouse.Target) -- fire remote event
end)
ok, now we have the part, lets delete it on the server, so that other players can see it
Script in ServerScriptService:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage.remoteEvent
local function isThisCharacterPart(part)
return part:FindFirstAncestorWhichIsA("Humanoid")
end
remoteEvent.OnServerEvent:Connect(function(player, part) -- when the remoteEvent fires server, it calls this function
if not part then return end -- checking if the part exists on server side
if isThisCharacterPart(part) or part.Name == "Baseplate" then return end -- we are preventing from deleting other player's parts or the baseplate
part:Destroy() -- now let's destroy this part
end)