Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how can I get the value of a string in a table?

Asked by
0msh 333 Moderation Voter
5 years ago

I know the title is very confusing, but how can I get the number value that equals a string value in a table?

01game: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
12end)

2 answers

Log in to vote
2
Answered by
cegberry 432 Moderation Voter
5 years ago
Edited 5 years ago

Doing a generic for loop can solve this problem:

1for _,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 
5end

Also your method is very insecure.

Ad
Log in to vote
1
Answered by
poke7667 142
5 years ago

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.

Answer this question