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
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
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
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.