Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make if a player presses 'R' he dies? Help me please.

Asked by 5 years ago

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.

0
Is this in a local script? Make sure it is and inside StarterPlayerScripts. You can also try handling player death using a remote event. Let the server handle the killing. xPolarium 1388 — 5y
0
Why do you post in comment not in answer? Anyways thank you. Salvestroni2 5 — 5y
0
I've tried putting in StarterPlayerScripts and as a local script, still doesn't work. Salvestroni2 5 — 5y
0
yea, have this script fire a remote event theking48989987 2147 — 5y
0
Sorry but how? I'm not experienced with FE scripting. Salvestroni2 5 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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

Ad
Log in to vote
0
Answered by 5 years ago

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

0
you know that humanoid changes replicate right User#19524 175 — 5y
0
Where shall I learn more about RemoteEvent and the RemoteFunction, what they do? Can you do it a little more simple? Salvestroni2 5 — 5y
0
If you are very new to lua, either watch SteadyOn's tutorials on youtube, he has 3 pretty good tutorials on remotes and FE or look at the wiki for remote events/ functions theking48989987 2147 — 5y

Answer this question