Hello, I was making something for my game, But an error happened, Could anyone fix this?
What I was trying to do is Clone the Tools to the Player's backpack, But this error happened:
"Workspace.Lucky_Blocks.Lucky_Block1.Script: attempt to index nil with backpack"
I don't know how to fix the script.
Script:
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local GearsFolder = game.ReplicatedStorage.Gears
script.Parent.Touched:Connect(function(hit)
local lucky = math.random(1,6)
if lucky == 1 then
GearsFolder.Bloxy_Cola:Clone().Parent = backpack
elseif lucky == 2 then
GearsFolder.Lightbox_Jar:Clone().Parent = backpack
elseif lucky == 3 then
GearsFolder.Panda_Friend:Clone().Parent = backpack
elseif lucky == 4 then
GearsFolder.Red_Rolling_Hoverboard:Clone().Parent = backpack
elseif lucky == 5 then
GearsFolder.Taco:Clone().Parent = backpack
elseif lucky == 6 then
GearsFolder.Toms_Beans:Clone().Parent = backpack
elseif lucky > 6 then
warn("Warning: Your unlucky.")
end
end)

2 comments
You cannot get the LocalPlayer on the Server. You can only do this in a LocalScript. but you can use the touched event parameter to get the player
In the touched event function do this local player = game.Players:GetPlayerFromCharacter(hit.Parent) add some sanity checks to make sure it's the character before adding the player code in