I know the title is very confusing, but how can I get the number value that equals a string value in a table?
01 | game:GetService( "ReplicatedStorage" ).Remote.Sell.OnServerEvent:Connect( function (plr, item) |
02 | local items = { |
03 | [ "Iron Sword" ] = 500 , |
04 | [ "Iron Greatsword" ] = 1000 , |
05 | } |
06 | for i = 1 ,#items do |
07 | if item = = items [ i ] then |
08 | plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value + items [ i ] .Value |
09 | plr.Backpack:FindFirstChild(item):Destroy() |
10 | end |
11 | end |
12 | end ) |
Doing a generic for loop can solve this problem:
1 | for _,object in pairs (plr.Backpack:GetChildren()) do -- Gets children of backpack |
2 | if items [ object.Name ] then -- checks if there is a item in the table that matches the name of the item |
3 | print ( "Found!" ) -- found |
4 |
5 | end |
Also your method is very insecure.
I would recommend putting the table outside of the function (although it is not needed, it's just a good practice). And to get a value from a table you have to use its index. Let's say you want to get the value stored in Iron Sword. You would do this: items["Iron Sword"]
. This will give u the value 500. I hope this helps you :D.