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

Attempt to index nil value error?

Asked by 4 years ago
Edited 4 years ago
if Explosion.Touched:Connect(function(plr)
    if AmmoD == 1 then
        if game.Players.LocalPlayer.Name ~= plr.Parent.Name then
            game.Workspace:FindFirstChild(plr.Parent.Name).Humanoid:TakeDamage(3)
        end
    end
    AmmoD = AmmoD - 1
end)

It worked and damaged the player but it only worked once and I was left with the error

0
Local or regular script? DanzLua 2879 — 4y
0
What line is the error shown? TheePBHST 154 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

I think I found your issue. First of all, you can't use an if statement on the first line of your code.

Secondly, I'm guessing this script is for a weapon, and line 3 is supposed to stop a player from damaging themselves. Therefore at line 3 of your script, if it isn't this isn't running on a local script, the 'LocalPlayer' won't be a valid variable. Therefore I would change line 3 to:

if game.Players:FindFirstChild(weaponHolder) ~= plr.Parent.Name then

The variable weaponHolder would be the string name of the player who has fired the weapon. You can change this to the variable you are using.

Another thing I saw that could be a potential issue is that Explosions don't use .Touched, they use .Hit to recognise a part that is in the radius of it. Therefore, if you are using Explosions, change line 1 to:

Explosion.Hit:Connect(function(plr)

Hope this helps

Tweakified

Ad

Answer this question