I'm using Input Service
My code is:
local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(input) if input.KeyCode == 'R' then game.Players.LocalPlayer.Character.Humanoid.Health = 0 end end)
I'm trying to learn how Input Service works.
IDK!!!!!!!!!!!!!!!!!!!
lol i do know, you have to make sure the LOCAL SCRIPT (it has to be a local script) is inside StarterPlayerScripts. Ur welcome. :D
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:
local UserInputService = game:GetService("UserInputService") local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") UserInputService.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.R then event:FireServer("KillPlayer") end end)
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
--server side local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") event.OnServerEvent:Connct(function(player,...) local otherArguments = {...} makes a table (array) of all the other arguments if otherArguments[1] and otherArguments[1] == "KillPlayer" then local humanoid = plr.Character:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then humanoid:TakeDamage(humanoid.Health)--basically kills the player end end end)
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