Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Workspace.Attach:13: attempt to index nil with 'FindFirstChild' ?

Asked by 3 months ago

Pretty much this is a RemoteEvent Script for Purchasing a bag from the shop, works but cannot find the Backpack on line 13

local attached = game.ReplicatedStorage.Events.Attached

local function attach(player)
    local stats = player.Bags
    local coins = player.leaderstats:FindFirstChild("????Coins")
        -- Variables 
    local character = script.Parent:FindFirstChild(player)
    local backpack = "Backpack"
    --
    wait()
    if stats.Equipped.Value == 1 then
        coins.Value = coins.Value - 500
        local oldbag = character:FindFirstChild(backpack)
        oldbag:Destroy()
        local torso = script.Parent:WaitForChild(player).Attach
        local weld = Instance.new("Weld")
        local clone = game.ReplicatedStorage.Backpacks.Backpacklvl2:Clone()
        clone.Parent = character
        clone.Name = "Backpack"
        weld.Parent = clone
        clone.Bag.CFrame = character.Attach.CFrame * CFrame.Angles(-165, math.pi, 55)
        weld.Part0 = clone.Bag
        weld.C0 = clone.Bag.CFrame:inverse()
        weld.Part1 = torso
        weld.C1 = torso.CFrame:inverse()
        clone.Bag.CanCollide = false
    end
end

attached.OnServerEvent:Connect(attach)

1 answer

Log in to vote
0
Answered by
KBRM101 383 Moderation Voter
3 months ago

There are 2 errors in your script which are both fairly simple to fix and make.

The first error is on line 7. You'll want to look for player.Name not player.

The next error is on line 13, you are looking for a backpack in the Character. If it returns nil, it cannot be destroyed. To fix this, you'd use an if statement:

if character:FindFirstChild("Backpack") then
     character.Backpack:Destroy()
else

end

This block of code would replace lines 13-14. The rest of your code will go after end.

I hope this answers your question

0
That was the solution, thank you. RemiIsDaBawss 2 — 3mo
0
If you don't mind, please accept my answer; it'll help us both out KBRM101 383 — 3mo
Ad

Answer this question