Tables instead if statements?

It there a way I can use a table to check which sword they bought instead of if & elseif statements?

ModuleScript
local swordsTable = {
	['TinSword'] = {
	['Price'] = 5
	},
	['CopperSword'] = {
	['Price'] = 10
	},
	['BronzeSword'] = {
	['Price'] = 15
	},
}
return swordsTable
local buyEvent = game.ReplicatedStorage.BuyEvent

local weapon = game.ReplicatedStorage.TinSword
local cost1 = 5
local cost2 = 10
local cost3 = 15

buyEvent.OnServerEvent:Connect(function(Player, Sword)
	local leaderstats = Player.leaderstats
	if Sword == 'TinSword' then
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value - cost1
		local weaponClone = weapon:Clone()
		weaponClone.Parent = Player.Backpack
	elseif Sword == 'CopperSword' then
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value - cost2
		local weaponClone = weapon.Parent.CopperSword:Clone()
		weaponClone.Parent = Player.Backpack
	elseif Sword == 'BronzeSword' then
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value - cost3
		local weaponClone = weapon.Parent.BronzeSword:Clone()
		weaponClone.Parent = Player.Backpack
	end
end)
2 Likes

You can iterate through the table and figure out which one matches the sword name:

local buyEvent = game.ReplicatedStorage.BuyEvent

local weapon = game.ReplicatedStorage.TinSword

buyEvent.OnServerEvent:Connect(function(Player, Sword)
	for key, value in pairs(swordsTable) do
		if key == Sword and Player.leaderstats.Points.Value <= value.Price then
			Player.leaderstats.Points.Value = Player.leaderstats.Points.Value - value.Price
			local weaponClone = game.ReplicatedStorage[key]:Clone()
			weaponClone.Parent = Player.Backpack
			return
		end
	end
end)

Something like this would probably work

buyEvent.OnServerEvent:Connect(function(Player, Sword)
	local leaderstats = Player.leaderstats
	for i,v in pairs(the table) do
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value - v.Price
		local weaponClone = weapon.Parent[v.Name]:Clone()
		weaponClone.Parent = Player.Backpack
	end
end)

Yes, you would require your ModuleScript and then check against the items in it.

local swordsTable = require(yourModule)
local buyEvent = game:GetService('ReplicatedStorage').BuyEvent

buyEvent.OnServerEvent:Connect(function(Player, Sword)
    local swordInfo = swordsTable[Sword] -- this gets the item from the table
    local leaderstats = Player.leaderstats

    if leaderstats.Points.Value >= swordInfo.Cost then -- you should always check if they can buy it on the server
        leaderstats.Points.Value = leaderstats.Points.Value - swordInfo.Cost
        swordInfo.Item:Clone().Parent = Player.Backpack
    end
end)

As for the location of the sword, in my example you’d store the path in the module under Item, so you can easily access it. Alternatively, you can index ReplicatedStorage using Sword if you want to keep all your items in the same place.

2 Likes

Goods looks homie.
Correct me if I’m wrong, but essentially what the script is doing;
It checks the name that is sent to the OnServerEvent to see which in the table has the same name, and
then it checks the value of the player’s points and compares it to the cost of the sword?

local swordInfo = swordsTable[Sword]

This line of code is where it compares the name?

1 Like

That line isn’t exactly a comparison; it’s a look up in the table.

local t = {a = 1, b = 2, c = 3}
local key = 'a'

print(t.a) -- 1
print(t['a']) -- 1
print(t[key]) -- 1

You use the name of the sword to look up the value in the table like you would using . syntax.

2 Likes