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.
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.
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.
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.
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.
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'
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:
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.
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.
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.