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
4 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?

game:GetService("ReplicatedStorage").Remote.Sell.OnServerEvent:Connect(function(plr, item)
    local items = {
    ["Iron Sword"] = 500,
    ["Iron Greatsword"] = 1000,
    }
    for i = 1,#items do
    if item == items[i] then
        plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value + items[i].Value
        plr.Backpack:FindFirstChild(item):Destroy()
    end
    end
end)

2 answers

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

Doing a generic for loop can solve this problem:

for _,object in pairs(plr.Backpack:GetChildren()) do -- Gets children of backpack
    if items[object.Name] then -- checks if there is a item in the table that matches the name of the item
    print("Found!") -- found

end

Also your method is very insecure.

Ad
Log in to vote
1
Answered by
poke7667 142
4 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