First, you should consider using the remote event. To start off, put a remote event into replicated storage and make a server script in server script service (or workspace) and a local script anywhere on the client: then on the local script, fire the remote event:
1 | local UserInputService = game:GetService( "UserInputService" ) |
2 | local event = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ) |
4 | UserInputService.InputBegan:Connect( function (input) |
5 | if input.KeyCode = = Enum.KeyCode.R then |
6 | event:FireServer( "KillPlayer" ) |
Note: if you only want the remote event to be used for this one thing, the "KillPlayer" argument isn't necessary to use, but if you want that one remote event to do many different things, I would advice keeping the argument "KillPlayer" as an if statement can be used on the server side to differentiate between different events
So, after the client fires the event, the server can get which player fired the event and any other arguments
02 | local event = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ) |
04 | event.OnServerEvent:Connct( function (player,...) |
05 | local otherArguments = { ... } makes a table (array) of all the other arguments |
06 | if otherArguments [ 1 ] and otherArguments [ 1 ] = = "KillPlayer" then |
07 | local humanoid = plr.Character:FindFirstChild( "Humanoid" ) |
08 | if humanoid and humanoid.Health > 0 then |
09 | humanoid:TakeDamage(humanoid.Health) |
so the script above checks if the first argument of otherArguments exists, then if it does, and the value of otherArguments[1] is equal to "KillPlayer", then it checks if the player's character, which the server can get, has a humanoid and that the player isn't dead. Then if all of those requirements are met, the player is killed