Im relatively new to scripting and this is probably a simple question but how would I damage a player when they press E on their keyboard?
How would I damage a player when they press E?
2 answers
@BrainDead_dev's answer is point the right the way, but the thing is, there's a feature called FilteringEnabled(FE) which makes his answer bad..
filtering enabled prevents localscripts from making changes that are visible to everyone else in the server, and this is essential to combat hackers, and exploiters..
therefore, everything that is to be replicated to all players in a game have to be done on the server.. roblox provides us with the RemoteEvents, and RemoteFunctions to do this
you can learn more about Remote Events and Remote Functions here, and on youtube - alvinblox, and also btw, since your beginner, his channel is great place to get started.. it has tutorials, and all sort of useful walkthroughs..
anyways, here's how you'd do it.
put a RemoteEvent in the ReplicatedStorage, name it\ "Damage" and do the following:
server script:
local ReplicatedStorage= game:GetService("ReplicatedStorage");
local remoteEvent = ReplicatedStorage:WaitForChild("Damage");
local damage = 20; --damage to take on a player
remoteEvent.OnServerEvent:Connect(function(player)
local character = player.Character;
if(not player) then return end;
local humanoid = character.Humanoid;
humanoid.Health = humanoid.Health - damage
end)
In a localscript:
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage= game:GetService("ReplicatedStorage");
local remoteEvent = ReplicatedStorage:WaitForChild("Damage");
UserInputService .InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
remoteEvent:FireServer()
end
end)
4 comments
this is pretty much what I did. How would mine not work with FE? It uses a RE and does pretty much the same thing. I probably made a dumb mistake or something.
Thanks! I did not know about this and that exploiters exploit like that. I will try both your solution and Brain Dead's but i dont really see much of a difference. I will make sure to Research this further.
@BrainDead_Dev huh, looks like you edited it.. but ur initial answer was flawed.. even now it's still flawed on the localscript side.. so relax and stop making ur answers about getting points, and make them about helping others
sorry, and where is my flaw in the local script. I just want to know for self improvement, and I agree your answer is way more descriptive than mine.
Add a remote event into workspace called "Damage" Add a local script into StarterGui. in it put this code :
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
local character = game.Players.LocalPlayer.Character
workspace.Damage:FireServer(character)
end
end)
Add a script under the remote event. Here is its code :
script.Parent.OnServerEvent:Connect(function(plr)
local char = plr.Character
char.Humanoid.Health = char.Humanoid.Health - 5
end)
5 comments
At least respect the client-server model
disrespect to FE ):
sorry let me quickly edit this
Edited
I think this will work, and i will check later. thanks
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
userinputservice and firing remotes and taking damage