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

I'm getting an error stating attempt to index nil with Backpack?

Asked by 3 years ago
Edited 3 years ago

This is a script located in serverscriptservice. All it does is once the gui called "Employer" has been clicked to send items it fires a remote event that will then clone the items from the person using the "Employer" Gui and give them to the person whose name has been written in plrname (plrname is a textbox). Please help! Thanks in advance!

01game.Players.PlayerAdded:Connect(function(player)
02game.ReplicatedStorage.fyg.OnServerEvent:Connect(function()
03    for i,v in pairs(game.Players:GetChildren()) do
04        local fu = v:FindFirstChild("PlayerGui")
05        local fa = fu:FindFirstChild("Employer")
06        local fe = fa:FindFirstChild("Frame")
07            local fo = fe:FindFirstChild("plrname")
08            local plr = game.Players:FindFirstChild(fo.Text)
09            local pack = plr.Backpack
10            for i,v in pairs(player.Backpack:GetChildren()) do
11                v:Clone().Parent = pack
12            end
13        end
14    end)
15end)

EDIT: the script to fire the remote event contains:

1script.Parent.MouseButton1Click:Connect(function()
2    local event1 = game.ReplicatedStorage.fyg
3    event1:FireServer()
4if game.ReplicatedStorage.orders.order.Value == 1 then
5        game.ReplicatedStorage.orders.order.Value = game.ReplicatedStorage.orders.order.Value -1
6    end
7end)

1 answer

Log in to vote
0
Answered by 3 years ago

You don't need the onserverevent event inside the playeradded event.

1game.ReplicatedStorage.fyg.OnServerEvent:Connect(function(plr)
2        local fu = plr:FindFirstChild("PlayerGui")
3        local fa = fu:FindFirstChild("Employer")
4        local fe = fa:FindFirstChild("Frame")
5        local pack = plr.Backpack
6        for i,v in pairs(player.Backpack:GetChildren()) do
7            v:Clone().Parent = pack
8        end
9 end)

The event returns the player so you don't need playeradded event to get the player which fired the remoteevent.

Ad

Answer this question