Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

2 comments

0

That was the solution, thank you.

0

If you don't mind, please accept my answer; it'll help us both out

This discussion is preserved as it was. New answers, comments, and votes are closed.