Item with Tables

I am a little stuck and need some help, with a command .add {user} {item}
I want it to add the item to the user and permanently have the item.

ex. .add itslightrblx ClassicSword

How would I go on to achieve this?

1 Like

You can use my admin system here. Set it up, then follow the packages section. Feel free to DM me if you have any questions.

2 Likes

I didn’t really want to use an admin system.

1 Like

This seems to have some bloatware.

1 Like

That’s fair, it was poorly designed. You might wanna follow this tutorial I made instead. You make your own command to clone the gear to the StarterPack.

1 Like

Not really what I was going for…

You’d have to make a few things.

  • Making a table (of course)

  • A folder of all of the possible weapons that is obtainable, or existing

  • And if you haven’t already, you’ll need to make a DataStore so that it will save the players inventory on Players.PlayerRemoving.

The set-up of these components should be something like this:

image

Then, inside of the script should be something like this:

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local ToolsToGet = ReplicatedStorage.Weapons
local WeaponDataStore = DataStoreService:GetDataStore('WeaponInventory')

--[[ 

	if you need help understanding what DataStore's are, go here:
	https://devforum.roblox.com/t/datastore-tutorial-for-beginners/733973

]]

-- This is the function to save the Player's data. Will be used later.

local function SaveData(Player)
	
	local WepFolder = Player:FindFirstChild("WeaponInventory")
	
	local weaponsSaved = {}
	
	for _,tools in ipairs(WepFolder:GetChildren()) do
		
		table.insert(weaponsSaved, tools.Name)
	end
	
	local success, errorr = pcall(function()
		WeaponDataStore:SetAsync(Player.UserId .. "_weapons", weaponsSaved)
	end)
	
	if success then
		print("Data saved.")
	else
		print("Data haven't been saved for this reason:")
		warn(errorr)
	end
end

-- Everything down here is functions for saving data. Skip to Line 92 for the command function.

Players.PlayerAdded:Connect(function(Player)
	
	local WepFolder = Instance.new("Folder")
	
	WepFolder.Name = "WeaponInventory"
	WepFolder.Parent = Player
	
	local wepData
	local success, errorr = pcall(function()
		wepData = WeaponDataStore:GetAsync(Player.UserId .. "_weapons")
	end)
	
	if success and wepData then
		
		for _,w in pairs(wepData) do
			
			local tool = ToolsToGet:FindFirstChild(w):Clone()
			
			if tool then
				tool.Parent = Player
			end
		end
		
	else
		print("Player must be new")
	end
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		SaveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(Players:GetPlayers()) do -- Loop through all the players, save their data.
		local success, err  = pcall(function()
			SaveData(player) -- Save the data (remember that function we made?)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 



-- This is where we make the command function. You can edit this however you want, if you understand what's going on here.

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		
		local commandArgs = message:split(" ") -- this is used to get the format of ".add {user} {item}"
		
		if commandArgs[1] == ".add" then
			
			local PlayerTarget
			
			for _,plr in ipairs(Players:GetPlayers()) do
				
				if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
					PlayerTarget = plr
				end
			end
			
			local ItemToGive
			
			for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
				if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
					ItemToGive = tool
				end
			end
			
			wait()
			
			if PlayerTarget and ItemToGive then
				
				local newItem = ItemToGive:Clone()
				
				newItem.Parent = PlayerTarget:FindFirstChild("WeaponInventory")
			end
		end
	end)
end)

[note while posting]: I did get warning 403 for not having the API Access enabled in Studio’s Security tab (made it in a blank baseplate locally), but that should still work since there’s no real errors. If there, is PM me or reply; whichever suits you.

You can edit it however you like, but this is just a base of how you can do it. If there’s questions let me know. If this helped though, don’t forget to mark as solution; Good luck on your project! <3

How would I remove it the item?

Make another command condition function below the “.add” function. Instead of it being “.add” make it “.remove”, and then make it so that it finds the item in the players inventory and just destroys it.

if commandArgs[1] == ".add" then
			
			local PlayerTarget
			
			for _,plr in ipairs(Players:GetPlayers()) do
				
				if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
					PlayerTarget = plr
				end
			end
			
			local ItemToGive
			
			for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
				if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
					ItemToGive = tool
				end
			end
			
			wait()
			
			if PlayerTarget and ItemToGive then
				
				local newItem = ItemToGive:Clone()
				
				newItem.Parent = PlayerTarget:FindFirstChild("WeaponInventory")
			end
		end

if commandArgs[1] == ".remove" then
			
			local PlayerTarget
			
			for _,plr in ipairs(Players:GetPlayers()) do
				
				if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
					PlayerTarget = plr
				end
			end
			
			local ItemToRemove
			
			for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
				if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
					ItemToRemove = tool
				end
			end
			
			wait()
			
			if PlayerTarget and ItemToRemove then
				
				ItemToRemove:Destroy()
			end
		end

Last thing is, how would I make a checking function?
ex. if player has classic sword in the db then

What would you mean by that? Because the function already does that to do the remove.

But, you can do the exact same process here:

if commandArgs[1] == ".add" then
			
			local PlayerTarget
			
			for _,plr in ipairs(Players:GetPlayers()) do
				
				if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
					PlayerTarget = plr
				end
			end
			
			local ItemToGive
			
			for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
				if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
					ItemToGive = tool
				end
			end
			
			wait()
			
			if PlayerTarget and ItemToGive then
				
				local newItem = ItemToGive:Clone()
				
				newItem.Parent = PlayerTarget:FindFirstChild("WeaponInventory")
			end
		end

if commandArgs[1] == ".remove" then
			
			local PlayerTarget
			
			for _,plr in ipairs(Players:GetPlayers()) do
				
				if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
					PlayerTarget = plr
				end
			end
			
			local ItemToRemove
			
			for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
				if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
					ItemToRemove = tool
				end
			end
			
			wait()
			
			if PlayerTarget and ItemToRemove then
				
				ItemToRemove:Destroy()
			end
		end

if commandArgs[1] == ".check" then
			
			local PlayerTarget
			
			for _,plr in ipairs(Players:GetPlayers()) do
				
				if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
					PlayerTarget = plr
				end
			end
			
			local ItemToFind
			
			for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
				if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
					ItemToFind = tool
				end
			end
			
			wait()
			
			if PlayerTarget and ItemToFind then
				
				print(PlayerTarget.Name .. " has " .. ItemToFind.Name .. " in their inventory.")
				-- your code here.
			end
		end

I want it to check if you have the item.

if player has the item then
print’hello’
locked.visible = false

Check the message above, edited.

1 Like

How do I make the command for some players only.

You have to make a table of a whitelisting system, for either the UserId (recommended to prevent any plagarism, since UserId’s are always unique) or Usernames.

At the top of where we had made the variables, should be a table as a variable for the whitelist. Something like this:

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local ToolsToGet = ReplicatedStorage.Weapons
local WeaponDataStore = DataStoreService:GetDataStore('WeaponInventory')

local PlayersWithPerm = { 1, 208608154, 1234567890 } 

--[[ for the UserId whitelist, just put the ID of a user here plainly without making it a string.
     if you just want to make it get usernames (not recommended), instead of the numbers you would have to make their usernames as strings
          e.g. local PlayersWithPerm = { "Roblox", "ItsLightRBLX", "Stas3860" } ]]

-- the rest of the code below ...

You’d use this new table for your command functions. Within the condition of checking the message if it’s a valid command, should also check to see if the player that messaged it is within the list.

Using the example of the UserId, instead of this:

if commandArgs[1] == ".add" then
-- the code
end

It will look like this:

if commandArgs[1] == ".add" and table.find(PlayersWithPerm, Player.UserId) then
-- the code
end

You’d have to put the and table.find(PlayersWithPerm, Player.UserId) in every command function next to it’s condition. OR, you can give it it’s own condition block, with the command functions inside of it.

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
      
            local commandArgs = message:split(" ")
            if table.find(PlayersWithPerm, Player.UserId) then
    
                if commandArgs[1] == ".add" then
			
	        		local PlayerTarget
			
	        		for _,plr in ipairs(Players:GetPlayers()) do
				
	    	    		if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
	    	    			PlayerTarget = plr
	    	    		end
	    	    	end
			
	    	    	local ItemToGive
			
	    	    	for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
	    	    		if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
	    	    			ItemToGive = tool
	    	    		end
	    	    	end
			
	    	    	wait()
			
	    	    	if PlayerTarget and ItemToGive then
				
	    	    		local newItem = ItemToGive:Clone()
				
	    	    		newItem.Parent = PlayerTarget:FindFirstChild("WeaponInventory")
	    	    	end
	    	    end

        if commandArgs[1] == ".remove" then
			
        			local PlayerTarget
			
    	    		for _,plr in ipairs(Players:GetPlayers()) do
				
    	    			if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
    	    				PlayerTarget = plr
    	    			end
    	    		end
			
    	    		local ItemToRemove
			
    	    		for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
    	    			if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
    	    				ItemToRemove = tool
    	    			end
    	    		end
			
    	    		wait()
			
    	    		if PlayerTarget and ItemToRemove then
				
    	    			ItemToRemove:Destroy()
    	    		end
    	    	end

        if commandArgs[1] == ".check" then
			
        			local PlayerTarget
			
    	    		for _,plr in ipairs(Players:GetPlayers()) do
				
    	    			if string.find(plr.Name:lower(), commandArgs[2]) or string.find(plr.Name:upper(), commandArgs[2]) or string.find(plr.Name, commandArgs[2]) then
    	    				PlayerTarget = plr
    	    			end
    	    		end
			
    	    		local ItemToFind
			
    	    		for _,tool in ipairs(ToolsToGet:GetChildren()) do
				
    	    			if string.find(tool.Name:lower(), commandArgs[3]) or string.find(tool.Name:upper(), commandArgs[3]) or string.find(tool.Name, commandArgs[3]) then
    	    				ItemToFind = tool
    	    			end
    	    		end
			
    	    		wait()
			
    	    		if PlayerTarget and ItemToFind then
				
    	    			print(PlayerTarget.Name .. " has " .. ItemToFind.Name .. " in their inventory.")
    	    			-- your code here.
    	        end
    	    end
        end
    end)
end)