I was trying to make it so that when you click on a cake, it gives you a cake to eat. When I try to click on the cake it gives me this error in the output bar: Workspace.ChocolateCake.GiveCake:6: attempt to index nil with ‘Backpack’ - Server - GiveCake:6
Here is my code: local ClickDetecter = script.Parent.ClickDetector local CakeTool = game.ReplicatedStorage.Food["Chocolate cake"]:Clone() local player = game.Players.LocalPlayer
local ClickDetecter = script.Parent.ClickDetector
ClickDetecter.MouseClick:Connect(function(player) -- MouseClick passes the player who clicked
local CakeTool = game.ReplicatedStorage.Food["Chocolate cake"]:Clone()
CakeTool.Parent = player.Backpack
end)
Add it in LUA boxs so we see it maybe a bit clear?
local ClickDetecter = script.Parent.ClickDetector
local CakeTool = game.ReplicatedStorage.Food["Chocolate cake"]:Clone()
local player = game.Players.LocalPlayer
ClickDetecter.MouseClick:Connect(function()
CakeTool.Parent = player.Backpack
end)
And, you define localPlayer in a script. Here is the fix:
local ClickDetector = script.Parent.ClickDetector
local CakeTool = game.ReplicastedStorage.Food["Chocolate cake"]
local player = game.Players
ClickDetector.MouseClick:Connect(function()
local CakeToolClone = CakeTool:Clone()
CakeToolClone.Parent = player.Backpack
end