Part won't delete when player presses "Delete"

I was following a tutorial to learn scripting, and ran into a problem. I’m pretty sure its a UserInputService problem because it never works for me.

  1. What do you want to achieve? I’m trying to make it so that when a player presses the Delete key, it removes a part for the whole server.

  2. What is the issue? Include screenshots / videos if possible!
    I pressed Delete, and nothing happened. Meanwhile, in theDevKing’s tutorial, he deleted it fine. And mine doesn’t do anything, no errors or anything. I think its a UserInputService problem because I have never gotten it to work.

  3. What solutions have you tried so far? Rewatching the video, rewriting my script many times, and I’ve tried to look for my problem everywhere.

Here is my code:
LocalScript inside StarterPlayerScripts:

local replicatedStorage = game:GetService("ReplicatedStorage")
local deletePart = replicatedStorage:WaitForChild("DeletePart")
local UIS = game:GetService("UserInputService")
local part = game.Workspace.plsDontDeleteMe

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Delete
	then
		print("Delete key pressed.")
		deletePart:FireServer(part)
	end
end)

Script inside ServerScriptService:

local replicatedStorage = game:GetService("ReplicatedStorage")
local deletePart = replicatedStorage:WaitForChild("DeletePart")

deletePart.OnServerEvent:Connect(function(player, part)
	part:Destroy()
end)

And there is also a remote event called “DeletePart” inside ReplicatedStorage. Please help me since UserInputService is something I’m going to be using a lot, but it just never works.

2 Likes

Is it printing or not deleting the part?

3 Likes

Tested it out and it worked fine, but if your keyboard has a numpad the delete button on there doesn’t send an input. (Atleast mine doesn’t) If you have multiple delete buttons on your keyboard try each of them.

2 Likes

I would try with the ContextActionService:

Wait, whats video?

3 Likes

Is the part created client-side by any local scripts? That might be why it does not delete.

3 Likes

I believe they’re referring to this video:

He says that he didn’t get any errors, if the part was created locally the server script would print an error because it can’t find “part”.
ServerScriptService.Script:5: attempt to index nil with 'Destroy'

2 Likes

Please, @matyas095 is right, is the script printing something or not? Otherwise you could add a print(part) on the server side and see if this prints something, so I would write something like this and see if anything was pressed or not:

local deletePart = game.ReplicatedStorage:WaitForChild("DeletePart")

deletePart.OnServerEvent:Connect(function(player, part)
        print(part, part:GetFullName()) --UPDATED
	part:Destroy()
end)
2 Likes

Look at properties when selecting it. Is anything showing up? If not, then your part isn’t there.

1 Like

It isn’t deleting or printing.

I’ll be sure to try that, but I’ve tested it out with other keys and it did nothing either.

Maybe if you try on another place this would work?

Turns out that I was pressing “Backspace” when it was clearly labeled “Delete”. I think this is because I have a mac, but I run a Windows OS on it so my Apple keyboard doesn’t label it as Backspace. The problem was so stupid but THANK YOU for finally fixing my problem! I think it works now, and I’ll be sure to actually use my brain before posting.

1 Like

By the way, you should add some kind of security check on the server side to make sure the part passed into the event is only a part that the player is allowed to delete. With this setup an exploiter could potentially spoof this remote event to delete any random instance in the workspace they want.

I was just following a tutorial to help me better understand, and doesn’t filtering enabled do that?

Filtering will stop activity on the client from replicating to the server, but in this case the client is explicitly telling the server to delete whatever object is passed into the remote event.

I wish I knew how I could stop that. Not that it’s a problem, because I haven’t published anything yet.