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

Search ServerStorage for an item from a table?

RS = game:GetService("ReplicatedStorage")
BuyItem = RS.Events.BuyItem

items = {
	["ShopItems"] = "ClassicSword","Axe","Pickaxe"
}

BuyItem.OnServerEvent:connect(function(player,cost,item)
	--subtracting money from player
	player.Money.Value = player.Money.Value -cost
	--giving them the item
	game.ServerStorage[items.ShopItems[item]]:Clone().Parent = player.Backpack
end)

So I'm trying to get the item the player is trying to buy and give it to them. I am storing the item names in a table so it's easier. I failed at doing it and I really don't know how to do this. I want to get the item from the table and search for it in serverstorage and give it to the player...

2 comments

0

You would have to loop through the table to find it, but imo it seems kind of unnessecary unless you intend to add values to that table like cost/damage/etc...is there a reason you can't just search serverstorage directly via FindFirstChild?

0

Yeah, I realised that.

2 answers

✓ Accepted answer

You don't search for the items every time someone clicks the purchase button. Instead you store it in the table first.

local ServerStorage = game:GetService("ServerStorage")
local RS = game:GetService("ReplicatedStorage")
local Events = RS:WaitForChild("Events")
local BuyItem = Events:WaitForChild("BuyItem")

-- Shop Items --
--Get them from your storage first
local ClassicSword = ServerStorage:WaitForChild("ClassicSword")
local Axe = ServerStorage:WaitForChild("Axe")
local Pickaxe = ServerStorage:WaitForChild("Pickaxe")

--Then store them in a table
local shopItems = {
	[ClassicSword.Name] = ClassicSword;
	[Axe.Name] = Axe;
	[Pickaxe.Name] = Pickaxe;
} 

local function Buy(player,cost,item)
    --subtracting money from player
    --player.Money.Value = player.Money.Value - cost
    --giving them the item
	local backpack = player:WaitForChild("Backpack")

    --Check if the shop item and backpack exists before you go inserting things
    if shopItems[item] and backpack then
		shopItems[item]:Clone().Parent = backpack
		print("Purchase successful")
	else
		print("Purchase failed")
	end 
end

game.Players.PlayerAdded:Connect(function(Player)
	Buy(Player,100,"Axe")	
	
	for itemName,item in pairs(shopItems) do
		print(tostring(itemName) .. " : " .. tostring(item) .. " is a " .. tostring(item.ClassName))
	end
end)

1 comment

1

Thanks!

There's a security concern with your RemoteEvent handling. Players can pass in the cost of an item, and you just trust it. Remember, exploiters can fire any remotes at any time with any arguments. This means that they could make the cost of an item 0. They could also use this event to award themselves money, by passing in a negative number.

I'd recommend constructing a table that maps item names to prices, and use that to securely implement this feature. You should also check if the item exists, not just assume that it does.

Additionally, RBXScriptSignal:connect() is deprecated, prefer RBXScriptSignal:Connect().

Variable identifiers like RS help no one, just use ReplicatedStorage or ReplicatedStorageService. Abbreviations like that serve no one, and we have autocomplete.

You should also consider putting items into a folder inside game.ServerStorage.

5 comments

0

Thanks for the tips. I just started learning remote events today.

0

Also, I was trying to use a table and figure out my error that I was having with my script...

0

No problem, if you feel that I adequately answered your question, feel free to hit the accept answer so this question is marked at solved.

0

Accept his answer and don't be a leech

0

@DaWarTekWizard He never answered my question lol...

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