Player Death on Dialog Choice

Hello! I was wondering how to make the player die once they reach a certain part of a dialog with another NPC.

This is my current code:

local dialog = workspace["Rubber Duck"].Dialog
local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")


local function onSelected(player, choice)
	if choice == dialog.DialogChoice.DialogChoice.DialogChoice.DialogChoice.Die then
		humanoid.Health = 0
	end
end

dialog.DialogChoiceSelected:Connect(onSelected)

The error read:

attempt to index nil with ‘Health’

(Im very new to scripting so please let me know what’s wrong with this script.
– Thank you)

You can not kill the player through a local script. Use a remote event to kill them on the server.

If you are unfamiliar with remote events here’s a simple tutorial:
Step 1)
create a RemoteEvent in ReplicatedStorage

Step 2)
name the remote event “KillSelf”

Step 3)

on the client replace “humanoid.Health = 0” with “game.ReplicatedStorage.KillSelf:FireServer()”

Step 4)

create a ServerScript in ServerScriptService and name it whatever you like

Step 5)

inside that script write

game.ReplicatedStorage.KillSelf.OnServerEvent:Connect(function(player))
local char = player.Character
local human = char:FindFirstChild("Humanoid")
human.Health = 0
end

Step 6)
run the game and see if it works if it doesnt lmk

I guess you can try adding this section of code in it:

plr.CharacterAdded:Connect(function(Chr)
    character = Chr
    humanoid = character:WaitForChild("Humanoid")
end

also:
Change

local humanoid = character:FindFirstChild("Humanoid")

to

local humanoid = character:WaitForChild("Humanoid")

that would fix part of the problem but keep in mind you cant kill yourself on the client.

modifying health on the client doesnt replicate

Just tested it and, as I suspected - you CAN kill the player with a local script.

its not like it errors but it doesnt replicate. if you dont believe me try your script with a friend

and if you think about it logically allowing players to deal damage to players on the client is a huge vulnerability.

Allowing a player to kill their own character isn’t much of a vulnerability. They can only kill their own player, NOT anyone else’s player

Also, if you are worried about vulnerabilities, your remote system would have more of a vulnerability, as players can spoof who it seems the request is coming from

bruh, now you are actually just wildin. have you even ever tried scripting in roblox before?

Based on number of correct responses to posts on the devforum, and success as a scripter in terms of visits, I’d say I am a bit more experienced in this than you are.

I created a simple test place, put a button in the player’s UI, and set the following code in a local script

script.Parent.Activated:Connect(function()
    game.Players.LocalPlayer.Character.Humanoid.Health = 0
end

Here is the result of my test:

nvm bruh sorry for gaslighting you. you can in fact kill yourself on the client by setting your health to zero. i was confused cause it doesnt work when the reset button is disabled which it is in the game im working on.

All good. At least we got to an eventual agreement from that