Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I fix the attempt to end index nil error in this script?

Alright, This is supposed to be a tool that kills the player. But it's telling me this: "attempted to index nil with Humanoid"

Here's the code:

local tool = script.Parent

local chr = game:GetService("Players").LocalPlayer:WaitForChild("Character")

script.Parent.Activated:Connect(function()
	chr.Humanoid.Health = 0
end)

Is it a hard fix or easy fix?

1 comment

0

No such error is possible with the code you've presented.

4 answers

its supposed to be LocalPlayer.Character also try to define the character inside the code block when the tool is activated so that way it doesnt glitch and the character is nil because that could also be another problem

Hi! Seems like you needed some help.

So I thought that changing a few things would help, try this script:

local tool = script.Parent

local chr = game.Players.LocalPlayer.Chraracter

script.Parent.Activated:Connect(function()
    chr.Humanoid.Health = 0
end)

Hope it works!

1 comment

0

Nope, It didn't work. Any other ideas?

Should I do it the dumb way instead?

Like :

script.Parent.Activated:Connect(function()
	script.Parent.Parent:WaitForChild("Humanoid").Health = 0
end)

I really hate this kind of scripts. But, I have to

You're calling plr.Character the wrong way. Yes, you need to wait for the Character to load, but it's supposed to be like this:

local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent

local chr = plr.Character or plr.CharacterAdded:Wait()

tool.Activated:Connect(function()
    chr.Humanoid.Health = 0
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.