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

Why is there an issue with indexing the players backpack in this script?

Asked by 3 years ago
local giver = script.Parent.Giver
local weapon = script.WeaponName

local function onGiverTouched(gamer)
    local earf = game.Players:FindFirstChild(gamer.Name)
    if earf.Backpack:FindFirstChild(weapon.Value) then

    else
        local clone = game.ReplicatedStorage:FindFirstChild(weapon.Value)
        clone:Clone()
        clone.Parent=game.Players:FindFirstChild(gamer.Name).Backpack
    end
    end
giver.Touched:Connect(onGiverTouched)

Im trying to create a simple gravity coil giver

Here's the error:Workspace.GravityCoilGiver.GiverScript:7: attempt to index nil with 'Backpack'

1 answer

Log in to vote
1
Answered by 3 years ago

The "Touched" event provides the part that touched the giver, not the player that contains it. Your script is attempting to find a player with the same name as said body part, which results in nil.

Simply replace gamer.Name on lines 5 and 11 with gamer.Parent.Name so the script can get the actual player name and find the backpack.

0
i would also check if the parenting is nil or not, this happens when the part thats touching gets removed by a service like debris service or even the destroy() service. Just a few friendly pointers :P TGazza 1336 — 3y
0
I feel like an idiot for not noticing, thanks for the help tightanfall 110 — 3y
Ad

Answer this question