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

Why does my script have errors like attempt to index nil with 'Backpack or Destroy'?

Asked by 3 years ago
Edited 3 years ago
01script.Parent.Touched:Connect(function(Char)
02    local plr = game.Players:GetPlayerFromCharacter(Char.Parent)
03    local backpack = plr.Backpack
04    local sword = game.Lighting.Storage.ClassicSword:Clone()
05    local con = game.Lighting.Storage.HasPvP:Clone()
06    if not backpack:FindFirstChild("HasPvP") then
07        sword.Parent = backpack
08        con.Parent = backpack
09        else
10        end
11end)
12 
13script.Parent.TouchEnded:Connect(function(Char)
14    local plr = game.Players:GetPlayerFromCharacter(Char.Parent)
15    local backpack = plr.Backpack
View all 22 lines...
0
Im trying to make a pvp zone in my game CrownedFigure 45 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

You're trying to access Backpack and the Destroy function of something that doesn't exist, so basically nil.Backpack and nil:Destroy(). I'm guessing that this part is touching another part of itself, and "plr" gets set to nil in line 2 and line 14 because the part doesn't have a player associated with it.

Just do a simple check to make sure "plr" isn't nil before the rest of the code executes:

01script.Parent.Touched:Connect(function(Char)
02    local plr = game.Players:GetPlayerFromCharacter(Char.Parent)
03    if plr then
04    local backpack = plr.Backpack
05        local sword = game.Lighting.Storage.ClassicSword:Clone()
06        local con = game.Lighting.Storage.HasPvP:Clone()
07        if not backpack:FindFirstChild("HasPvP") then
08            sword.Parent = backpack
09            con.Parent = backpack
10            end -- also you don't need an else block here if it's just empty
11    end
12end)
13 
14-- do the same thing in the TouchEnded event

you should probably also put the sword in ReplicatedStorage or ServerStorage because having models mixed up with postprocessing effects and all that will be an organization nightmare later on

Ad

Answer this question