Help with tables

Hello, so I am wondering how could I loop through this “Tools” table to constantly check if a player has a tool in Server Storage and if they do it will fire an event to a local script saying that they own it?

local ArmoryItems = game.ServerStorage.ArmoryItems
local OwnsEvent = game.ReplicatedStorage.ArmoryPurchases.HasTool

local Tools = {
	"Sniper",
	"Sword",
	"WaterGun",
}

game.Players.PlayerAdded:Connect(function(plr)
	if table.find(ArmoryItems) then
		OwnsEvent:FireClient()
	end
end)

You could loop thru and check like this:

task.spawn(function()
for i, tool in pairs(Tools) do
for i, item in pairs(Player.Backpack:GetChildren()) do
if item.Name == tool then
OwnsEvent:FireClient()
     end
  end
end
end)

I’m pretty sure this would go in your PlayerAdded event.

You could also replace task.spawn with a coroutine but I prefer task.spawn most of the time

I cant get the players backpack though can I? Since its a server script

1 Like

You can I’m pretty sure (filler stuff yknow CCC).

Im not the best at scripting, so how would I be able to do that with the script you gave me?

1 Like

Is it not working when you re referencing Player.Backpack (I believe it should).

No its not im not sure why (CCC)

Hm, is the for loop not running or is it doing something else (like erroring)?

Why don’t you give it a try? On the actual API it says you can. Backpack | Documentation

The Backpack can be accessed from both the client and the server.

-- Accessing Backpack from a Server Script:
game.Players.PlayerName.Backpack

-- Accessing Backpack from a LocalScript:
game.Players.LocalPlayer.Backpack

Also you want to check if the player has a certain tool every X amount of time? Or just when they are added to the game? Also are you getting any error messages? More details please.

1 Like

Alright so I want a loop that can constantly check if a user does or doesnt have an item for the reason being when I fire the event I can change the proximity text to “Owned”. This system is for my item shop. I do have this error message PlayerName is not a valid member of Players "Players" - Server - ArmoryPurchaseHandler:13

local ArmoryItems = game.ServerStorage.ArmoryItems
local OwnsEvent = game.ReplicatedStorage.ArmoryPurchases.HasTool


local Tools = {
	"Sniper",
	"Sword",
	"WaterGun",
}

task.spawn(function()
	for i, tool in pairs(Tools) do
		for i, item in pairs(game.Players.PlayerName.Backpack:GetChildren()) do
			if item.Name == tool then
				OwnsEvent:FireClient()
			end
		end
	end
end)

If you want to contact me on discord Id be glad if you could make a simple system. Ill pay robux for however much. If not thats cool

I’m not going to give you the answer, but I highly recommend using the ChildAdded function in addition with checking the ClassName & Name with some if statements. So for example…

PlayerAdded:Connect(function(player)
    CharacterAdded...
         Character.ChildAdded...
               if ........ then
                   Fire Event
               end
          end

          Player.Backpack.ChildAdded...
               if ........ then
                   Fire Event
               end
          end
    end
end
1 Like

You’re trying to reference a variable that is nil

game.Players.PlayerName

this does not exist unless your name on Roblox is actually “PlayerName”.
just reference each player instead.

Also if you’re trying to search every player in the game you need to first iterate through all the players which is easy with the PlayerService API.

for i, player in pairs(Players:GetPlayers()) do
    print(player.Name)
end

If you want to continually search through every player and then check their tools to see if they owned any thats listed in their Backpack you will need to wrap this inside a infinite loop which is not good practice because there are better ways to consume way less resources and get same results.

local UPDATE_TIME = 0.2
task.spawn(function()
	
	while true do
		for _, player in pairs(Players:GetPlayers()) do
			for i, tool in pairs(Tools) do
				for index, item in pairs(player.Backpack:GetChildren()) do
					print("index: "..index)
					print("item: "..item.Name)
					if item.Name == tool then
						print("BOOM...")
						--OwnsEvent:FireClient()
					end
				end
			end
		print(player.Name)
		end
		wait(UPDATE_TIME)
	end
	
end)

LocalScript in StarterPlayer.StarterPlayerScripts

task.wait(3) -- time to load
local player = game.Players.LocalPlayer
local backpack = player.Backpack

local function onChildAdded(child)
	if child.Name == "WhateverTool" then
		-- do whatever
	end
end

backpack.ChildAdded:Connect(onChildAdded)

I’m not sure if you accidently replied to me or what but what have you posted that wasn’t already posted?

Also, why would he use a module script just to return the local player when he can just grab the player as he is iterating through it on the server? Looks like you copied and pasted and just renamed some identifiers. Also when you describe things to someone that you think need to be added it would be better to not shorten the name so that anyone reading what you wrote doesn’t guess what you’re talking about. Cooldown is a lot easier to understand then cd…