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

I am getting an error with ServerStorage and my table?

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


--Shop Items--
local ClassicSword = ServerStorage.Items:WaitForChild("ClassicSword")

--In a Table--
local shopItems = {
	[ClassicSword] = {
			Price = 500,
					
	},
}
	
BuyItem.OnServerEvent:Connect(function(player,item)
	--vars
	local backpack = player:WaitForChild("Backpack")
	--checking for item
	if shopItems[item] and backpack then
		ServerStorage.Items[shopItems[item]]:Clone().Parent = backpack
		player.Money.Value = player.Money.Value -0
		print("Purchased Item!")
	else
		print("Purchase failed!")
	end
end)

So I am trying to use a table to secure my remotes a little bit more and it's a lot more organized. Anyways, the problem I am having is I'm getting an error. I get "Purchase Failed" The ClassicSword is clearly in the table... why am I getting purchase failed?

1 comment

0

Put ClassicSword into " quotes or remove the [] brackets around it

1 answer

✓ Accepted answer

Great job on securing your remotes. The backpack variable will never be nil, so you don't need to check for that.

References to instances can only be sent through remotes when the instance itself is visible to both the server and clients. Clients can't see your sword in game.ServerStorage, so they can't send it through a remote.

Consider having clients send the item name through the remote, and store item names as keys, and the value can be a table of item data like this:

local shopItems = {
	ClassicSword = {
		Tool = game.ServerStorage.Tool,
		Price = 500
	}
}

1 comment

0

Thanks!

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