Script doesnt give gun to backpack

Hi, I want to make that if you click on button, it gives you a gun.

local gun = game.ReplicatedStorage.pistolpewpew
game.Players.PlayerAdded:Connect(function(player)
	local Character = player.Character
	local backpack = Character:FindFirstChild("Backpack")
	script.Parent.MouseButton1Click:Connect(function()
		gun.Parent = backpack
	end)
end)

Localscript is in button part. Can someone help me?

I think it’s because the backpack is inside of the player, not the character.

try using this instead:
`local backpack = player:FindFirstChild(ā€œBackpackā€)

didn’t realize you said localscript.

2 Likes

It didnt work

dfdsfsdfsdfddsfds

local gun = game:WaitForChild("ReplicatedStorage").pistolpewpew
game.Players.PlayerAdded:Connect(function(player)
	local Character = player.Character
	script.Parent.MouseButton1Click:Connect(function()
		gun:Clone().Parent = player.Backpack
	end)
end)
1 Like

local scripts do not run in workspace
image
And it’s ridiculous that you give a weapon in client
(i used translate)

1 Like

HišŸ‘‹,

Usually game.Players.PlayerAdded is not used in a LocalScript but Script.

Here’s some examples of what you could do.

  1. RemoteEvent
  • Add a RemoteEvent inside ReplicatedStorage and call it, for exampleGiveItem

  • Add a script inside of ServerScriptService and type this code:

game.ReplicatedStorage.GiveItem.OnServerEvent:Connect(function(plr, tool)
     if plr then
          tool.Parent = plr.Backpack
     end
end)
  • For the LocalScript, type this code:
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.GiveItem:FireServer("item") 
end)
  1. Do your Method but different.
  • Add a script inside ServerScriptService and type this code:
game.Players.PlayerAdded:Connect(function(plr)
     "replace this with your button location".MouseButton1Click:Connect(function() 

game.ReplicatedStorage.pistolpewpew.Parent = plr.Backpack
     end)
end)

I hope this helped! :sweat_smile:
If it does not work right, feel free to ask for help.
I did this on my phone so it will look Messy.

Goodluck!:four_leaf_clover:

2 Likes

Gui button isnt in workspace, it is in startergui

I used first method, but it gives me this error: " ServerScriptService.Script:3: attempt to index string with ā€˜Parent’ "

You have to type instead of ā€œitemā€ type the location of your gun

Thanks, it is working now.

dsadasddsaadad

1 Like

One question and Im good. How do I make so it gives me clone of this?

game.ReplicatedStorage.GiveItem.OnServerEvent:Connect(function(plr, tool)
     if plr then
          tool:Clone().Parent = plr.Backpack
     end
end)

Also, no problem.

1 Like

Thank you!

dfssfsfsadsadssadsada

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.