Having Problems with r to death

Hi Devs so simply was making r to death system

Here is my code

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.R then
			local plr = game.Players.LocalPlayer
			workspace.plr.Humanoid.Health = 0
		end
	end
end)

Dont Mind workspace.plr i tried WaitForChild too and it doesnt help

1 Like

workspace.plr doesn’t quite work, as plr is not an instance in workspace. You will need to use plr.Character instead to appropriately and expectedly find the humanoid.

1 Like

So i updated my code a little bit and still not working

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.R then
			local player = game.Players.LocalPlayer
			local character = player.Character
			if not character or not character.Parent then
				character = player.CharacterAdded:wait()
			end
			workspace:WaitForChild(character):FindFirstChild("Humanoid").Health = 0
		end
	end
end)

What i am doing Wrong and Thanks for telling me one of my problems

Try replacing:

workspace:WaitForChild(character):FindFirstChild("Humanoid").Health = 0

with

character:FindFirstChild("Humanoid").Health = 0
2 Likes

Finnaly Thanks for your help much appreciated.

(Really stupid Mistake :crazy_face:)

1 Like

Hey there, I am personally not a experienced scripter but here is an example of a code I use for one of my games and it works perfectly.

local m = game.Players.LocalPlayer:GetMouse()
db = true
m.KeyDown:connect(function(r)
	r = r:lower()
	if r == "r" then
		if db == true then
			game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").Health = 0
		end
	end
end)

This I keep under StarterPlayer > StarterCharacterScripts.

Hope it helps you.

Not to sound rude or anything but mouse.KeyDown is deprecated and you should never use it. You should use UserInputService instead.

1 Like

I guess I am using a outdated code then, I’ll make sure to fix it up a bit.

Thanks.

1 Like

If you put instance instead of string on FindFirstChild() method first parameter, it would never work. I already test that.

Do you want this to replicate to the server or just the client? If you want to replicate it to the server then use a remote event.

I don’t think you should be using UIS for resetting under no conditions, I’d recommend using ContextActionService, this is to avoid the same issue this guy had: