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
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)
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
5 comments
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.
Why do you post in comment not in answer? Anyways thank you.
I've tried putting in StarterPlayerScripts and as a local script, still doesn't work.
yea, have this script fire a remote event
Sorry but how? I'm not experienced with FE scripting.