Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 1 year ago

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?

0
No such error is possible with the code you've presented. Ziffixture 6913 — 1y

4 answers

Log in to vote
0
Answered by 1 year ago

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

Ad
Log in to vote
0
Answered by 1 year ago

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!

0
Nope, It didn't work. Any other ideas? Essern_ALT 5 — 1y
Log in to vote
0
Answered by 1 year ago

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

Log in to vote
0
Answered by 1 year ago

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)

Answer this question