Function won't work

so this function won’t work when I have the variables and stuff it is a script to find if the player has a tool if it doesn’t then it should kill him here is the script:

local player = game.Players.LocalPlayer
local Key = game.Workspace.Key

local function key()
if player.Backpack.Key then
player.Humanoid.Health = 1
else
player.Humanoid.Health = 0
end
end

key()

1 Like

Are you getting any errors in your output?

This function is suppose to be a 1 time thing not a whole loop

1 Like

do you have it firing at any specific interaction?

If I want to be more detail I want the script to wait(60) then have the script run.

A bit revised:

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

local Backpack = player:WaitForChild("Backpack")
local Humanoid = Character:WaitForChild("Humanoid")

local Key = game.Workspace.Key

local function key()
    if Backpack:FindFirstChild("Key") then
        Humanoid.Health = 1
    else
        Humanoid.Health = 0
    end
end

wait(60)
key()

Couple things as well:

  • Is this a LocalScript or a Server Script? Server Scripts aren’t handle to detect the LocalPlayer

  • The Player and Character are 2 different things, the Player would be the current player that’s in the game, and the Character is actually the Character Model that spawns in your game

  • If you want to search for a specific thing, use FindFirstChild which will check if that specific name is inside the Player’s Backpack or not once 60 seconds pass

  • Are there any errors on your Output at all?

1 Like

Local script and errors:

Can you try out the script I sent you? Put it in StarterPlayerScripts and see what happens

1 Like

The script actually works and I put it in my Gui I need it for thank you!

Uhhhhhhhh now its not working it killed me right when I was holding the tool and nothing in the output

It actually took my health away also having the tool in the backpack and I am surviving with 0 health somehow…

Oh

So, what actually happens is that when you actually equip a tool, it’s actually reparented to your Character’s Model so it actually leaves the Backpack

To fix that, add another conditional check if the Tool is either in the Backpack, or in the Character Model:

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

local Backpack = player:WaitForChild("Backpack")
local Humanoid = Character:WaitForChild("Humanoid")

local Key = game.Workspace.Key

local function key()
    if Backpack:FindFirstChild("Key") or Character:FindFirstChild("Key") then
        Humanoid.Health = 1
    else
        Humanoid.Health = 0
    end
end

wait(60)
key()

It still just takes away my health even when I have the tool in my character or backpack and it doesn’t kill me it just takes my health to 0 and I somehow am alive from that.

It’s cause the script is setting your own Character’s Health to 1 whenever it detects that you have a Key, but doesn’t kill you

If you don’t have a Key, your HP sets to 0 which is the kill function by default

I’m not quite sure I follow your scenario here, did you also want to adjust the MaxHealth of your Character as well?

Its on default roblox health I didn’t change it