Need help putting a tool into a players backpack, then finding it

Player Backpack Help

Hello! So I have a little issue here. Here is what I am trying to achieve: your team when you get in-game is neutral, but when you chat “:Team me Security” It will put you into the “Security” team. This much works. This is a LocalScript put in StarterPlayerScripts. Then in the same script, I have this code that will check if a player is added so then it can give them a keycard that gives them access to certain buildings/ through security.

teams:FindFirstChild("Police").PlayerAdded:Connect(function(player)
	print("Got Team")
	local PoliceKeycard = game.ReplicatedStorage:FindFirstChild("PoliceKeycard"):Clone()
	print("Cloned")
	PoliceKeycard.Parent = player.Backpack
	print("Gave Keycard")
end)

This much works, when I test the game and say that, it puts a “PoliceKeycard” into my backpack. When I do Print(player.Backpack:GetChildren()), It prints everything in the backpack EXCEPT for the keycard. But then when I go into the backpack, it is there, so I do not know why it says it isn’t. Here is the code that is checking for the keycard. (also teams is a variable for teams = game:GetService(“Teams”))

local KeyCardCheckOne = game.workspace.KeyCardCheckOne
KeyCardCheckOne.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player then
		print(player.Backpack:GetChildren())
		if player.Backpack:FindFirstChild("PoliceKeycard") or player.Character:FindFirstChild("PoliceKeycard") then
			print("You have the keycard. You may Pass")
		else
			print("You Do not have the keycard. You may not pass.")
		end
	end
end)

When I do not have the keycard, it prints I don’t have it. When I team myself police and I can see it in my backpack, it still says it isn’t there. Also, I put the “or” because when a tool is equipped it goes into the Character.

So can anyone help me with this? What am I doing wrong?

If you’re using a LocalScript then all of your changes will not be replicated to the server.

Thats what I thought at first, so i made it a server script and I put it in serverscriptstorage, but then it kept saying “Attempt to index nil with chatted” so It cant get the player, thats why I tried putting it in the local script.

Is the first script a local script? If yes then fire a remote event in that function, and put the cloning inside a server script in an OnServerEvent function.

1 Like

Thank you it worked, I’ll keep this in mind for the future so I don’t make that mistake again. :slight_smile:

1 Like

Then you have to fix that error in order for it to work. I assume the check happens on the server. If the card giving happens in a local script then the server won’t be able to see it.