Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.