|
| 19 Aug 2016 01:44 PM |
So basically I have a players inventory stored in one string. I use string:Match to see if they own an item. However, I would like to add where then can own duplicate items. Is there a way I can see how many times a string matched the parameters?
Thanks. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 01:54 PM |
I'd redo your system. Now, I would store all the information in a table, for ex.
inventory = {"Blade","Blade1","Blade","Blade4"};
table.insert(inventory,item)
Use some sort of 'for' statement to transfer the datastore to the actual table. I'll try to test something myself, and give it to you.
~ The Lonely Developer |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 01:56 PM |
Tables crashed my last system.
Thanks though.
Any way I can do the string thing? |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 01:58 PM |
if tables crashed your system then youre doing something wrong
|
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:00 PM |
name = "Inventory"; local DataStore = game:GetService("DataStoreService"):GetDataStore(name); newinventory = {"Sword"}; inventory = {};
game.Players.PlayerAdded:connect(function(plyr)
if(DataStore:GetAsync(plyr.Name) == nil) then
DataStore:SetAsync(plyr.Name, newInventory);
end
-- load data store items = DataStore:GetAsync(plyr.Name); for i = 1,#items do
if(items[i] ~= nil) then
table.insert(inventory,items[i]);
end
end
end)
-- Add Item function addItem(IT, plyr) name = "Inventory"; local DataStore = game:GetService("DataStoreService"):GetDataStore(name); newitem = IT; currentinv = DataStore:GetAsync(plyr); table.insert(currentinv,newitem); DataStore:SetAsync(plyr,currentinv); end
-- IT is item name, PLYR is a STRING of the players name
~ The Lonely Developer |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:00 PM |
Didn't test this, I have never made an inventory system, just a scripter. But, you get the idea.
~ The Lonely Developer |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:04 PM |
Adding to the previous post, Now you can get items.
-- code
inventory = {}; for i = 1,#inventory do
if(inventory[i] ~= nil) then
-- change to whereever u store your items ITEM = game.ServerStorage:FindFirstChild(inventory[i]):Clone(); if(ITEM == nil) then print("error"); return; end BP = game.Players.LocalPlayer.Backpack; ITEM.Parent = BP;
end
end
~ The Lonely Developer |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:05 PM |
Finally relating to your post, strings won't work.
~ The Lonely Developer |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:06 PM |
| Tables broke last time because I was accessing the data store too many times in a short amount of time. Is it possible for me to store their data in the game somewhere after I load if the first time for faster access? |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:06 PM |
To answer the OP's original question:
"string.gsub[edit] string.gsub (s, pattern, repl [, n])
Returns a copy of s in which all (or the first n, if given) occurrences of pattern have been replaced by replacement string repl, which may be a string, a table, or a function. string.gsub also returns, as its second value, the total number of matches that occurred."
Here's some pseudo code that represents approximately what you should do with that information:
select( 2, inventoryString:gsub( pattern, '' ) ) |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:08 PM |
| @Tillo, he can use strings if he wants, albeit it's not as efficient as simply using tables and not constantly polling the Data Store for updated information. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:11 PM |
Even if this does work, nearly 99% of any other developer would use this method. Though, instead of DataStores they would use HttpService to hook up to their own server, so they can send more requests.
~ The Lonely Developer |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:17 PM |
| It doesn't matter if everyone else does it differently, he can make it however he wants. That's the beauty of programming. It may not be the most efficient or elegant solution but it can still be feasible. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:17 PM |
Oh, and you would store the datastore in a value and add to it that way, then when a player leaves the server you would save it to minimize requests.
~ The Lonely Developer |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2016 02:19 PM |
for w in string.gmatch("String","Match") do
print(w)
or it's string.gsub
Formerly xXTheRobotXx, add 13,349 posts |
|
|
| Report Abuse |
|
|