Error with making player die when the player presses R

I’m trying to make a script so that you die when you press R, but I get an error. This is the error

[Players.Bliksem18.PlayerScripts.RToReset:7 attempt to index nil with ‘Humanoid’]

And this is the script I use. It is in a LocalScript.

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character

UIS.InputBegan:Connect(function(input,gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.R then
		character.Humanoid.Health = 0
	end
end)

I am very new to scripting and Roblox developing. It would be great if someone could help me.

1 Like

Its because the character isnt fully added in the moment you made this script, if its in a local script then you can put it in starter character scripts and switch the variable of character to script.Parent

If you dont want to do that then you can just switch the character variable to this:

local character = player.Character or player.CharacterAdded:Wait()
2 Likes

I put it in the StarterCharacterScripts and it worked! Thank you for this.

2 Likes

Just a thing but you can use the second alternative I gave you for the server scripts. Anyways, No problem.