How exactly would I kill the player in this situation?

Hi! I was working on a soda that kills you 0.5 seconds after you equip it. However, I just can’t seem to find a solution. Here’s my code so far:

local Press = Enum.KeyCode.E --- Variable to hold the letter I want

local UserInputService = game:GetService("UserInputService") --- InputService for Player

local Tool = game.StarterPack.Tool --- Finds The Tool in starterpack

local Character = game.Character --- The Players model

local Humanoid = Character.Humanoid --- The root part


local equipped = false
local tool = script.Parent

tool.Equipped:Connect(function()

equipped = true

game.Workspace.Sound:Play()

wait(0.5)


--- Trying To Kill The player Here, Deleted the old code cuz it was a mess

  wait(0.5)

end)

tool.Unequipped:Connect(function()
equipped = false
game.Workspace.Sound:Stop()
 end)

Please help!

You would want to put

local Hum = tool.Parent.Humanoid
Hum.Health -= 100

It’s better to use the TakeDamage method, or set the health to zero, as maybe the humanoid might have a bigger MaxHealth than the default

1 Like

You are totally correct.

local Hum = tool.Parent.Humanoid
Hum.Health -= Hum.MaxHealth

Here is the better solution :slight_smile:

Was I suppose to put a humanoid inside the tool? Because it wants me to put a humanoid in starterpack. Somethings probably wrong because it won’t kill me :confused:

Try this

tool.Equipped:Connect(function()

if tool.Parent:FindFirstChild("Humanoid") then
    equipped = true
    game.Workspace.Sound:Play()
    wait(0.5)
    local Hum = tool.Parent.Humanoid
    Hum.Health -= Hum.MaxHealth
    wait(0.5)

end)

Errors are coming from the end) and the other end at the very bottom of the code. I think that the code thinks it’s still in the loop, let me check.

This is a basic error that you should easily know. Close off the if statement with an end

There is an if statement closing off the if statement though. Same with the bottom one. Says that it needs an end at the end of this line:

tool.Equipped:Connect(function()

Wouldn’t that just end the whole statement below it or would it keep running? I haven’t done scripting in months so i’m really bad at the moment.

This should do the job. The error is saying that the if statement wasn’t closed off (which was my mistake)

  00:13:03.643  Humanoid is not a valid member of StarterPack "StarterPack"  -  Server - Script:7

I have never seen this error in the client before. Not sure what to do about this. This is kinda what I referenced above:

Hope this helps!

(Ps. I’m really bad at scripting please don’t judge.)

I STRONGLY suggest learning basic errors for Roblox scripting.

Try this:

repeat wait() until tool.Parent ~= game:GetService("StarterPack")

tool.Equipped:Connect(function()

if tool.Parent:FindFirstChild("Humanoid") then
    equipped = true
    game.Workspace.Sound:Play()
    wait(0.5)
    local Hum = tool.Parent.Humanoid
    Hum.Health -= Hum.MaxHealth
    wait(0.5)
end
end)

I have like 10 tabs open about scripting a day to learn it and I still haven’t learned the basics :upside_down_face:

I just found the scource of the error. It’s the variable referencing Hum. It keeps wanting to use a humanoid in starterpack when the player isn’t even related to that. However, I would have to find another scource instead of parent. Is there an alternative to parent?

local Hum = Tool.Parent.Humanoid

If you do however place a humanoid in starterpack, the sound plays instead of it not playing. So that’s a bonus I guess.

Edit: It’s just the Tool.Parent part of the line that causes the error

1 Like

A humanoid is in a player’s character. When referencing Hum i am referring to the player’s character humanoid.

Also, make sure that the actual tool makes it into the players backpack.

Tysm it worked! :grinning: :grinning: :grinning:

Glad it worked! I hope you do well on your journey of learning to program!

1 Like

The problem here is the referencing of your tool. A tool in the StarterPack will be cloned into the Player’s Backpack when they join the game. This allows them to see the black transparent box everyone knows as a tool. When this tool is clicked the actual tool object will be parented to the player’s character. Now that we understand how tools work let’s fix your code.

  1. Make sure the script you are using is a local script parented to the tool in studio. The Tool’s parent should be StarterPack.
  2. Change the Tool variable to:
    ‘’’
    Tool = script.Parent
    ‘’’
    That is all. Don’t forget to mark this as a solution if it helped! :slight_smile:

Hey man! Thanks for the advice! Someone else found the solution though :frowning: However, tysm for helping me as well! :sunglasses: :sunglasses: :sunglasses:

1 Like