Buy button not working

  1. What do I want to achieve? I want to have a proper buy button

  2. What is the issue? It is showing this error

itzmerose_12 is not a valid member of Folder "ServerStorage.OwnedItems"
  1. What solutions have you tried so far? I looked it in youtube and searched in dev forum
--Server side
local rep = game:GetService("ReplicatedStorage")
local items = game:GetService("ServerStorage"):WaitForChild("Tools")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name ="leaderstats"
	stats.Parent = player
	
	local Strength = Instance.new("NumberValue")
	Strength.Name ="Strength"
	Strength.Parent = stats
	
	local Coins = Instance.new("NumberValue")
	Coins.Name ="Coins"
	Coins.Parent = stats
	Coins.Value = 49
	
	local gems = Instance.new("NumberValue", stats)
	gems.Name = "Gems"
	
	local storage = Instance.new("NumberValue")
	storage.Value = 10
	storage.Name = "Storage"
	storage.Parent = player
	
	local Class = Instance.new("StringValue")
	Class.Name = "Class"
	Class.Parent = stats
	
	
	local items = Instance.new("Folder", player)
	items.Name = "OwnedItems"
end)
rep.Remotes.info.OnServerInvoke = function(player , item)
	return items[item].Cost.Value
end
game:GetService("ReplicatedStorage").Remotes.sale.OnServerInvoke = function(player , item)
	local playergui = player.PlayerGui
	print(player.Name.."Invoked server")
	price = items[item].Cost.Value
		
	if player.leaderstats.Coins.Value >= price and not game.ServerStorage.OwnedItems[player.Name]:FindFirstChild(item)then 
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
		
		player.Character.Humanoid:UnequipTools()
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool.Name == player.Equipped.Value then
				tool:Destroy()
			end
		endl
		player.Equipped.Value = item
		local gear = items[item]:Clone()
		gear.Parent = game.ServerStorage.OwnedItems

		local gear = items[item]:Clone()
		gear.Parent = player.Backpack
		return true
	elseif price > player.leaderstats.Coins.Value and not game.ServerStorage.OwnedItems[player.Name]:FindFirstChild(item) then
		return "Not enough"
	else
		return "Bought already"
	end
	
end
game.ReplicatedStorage.Remotes.toolbought.OnServerInvoke = function(player, item)
	local tools = {}
	
	for i,tool in pairs(game.ServerStorage.OwnedItems[player.Name]:GetChildren()) do
		table.insert(tools, tool.Name)
	end
	return tools
end

--Client side

local rep = game:GetService("ReplicatedStorage")
local itemsel = script.Parent.Parent.selecter
local selecter = script.Parent.Parent.selecter
local cantbuy = script.Parent.Parent.cant_buy
local status = script.Parent.Parent.status
local outcome = rep.Remotes.sale:InvokeServer(selecter.Value)

script.Parent.MouseButton1Click:Connect(function()
	local outcome = rep.Remotes.sale:InvokeServer(selecter.Value)
	if outcome == true then
		status.Text = "Equipped"
		
	elseif outcome == "not enough" then
		print("YEET")
	
	elseif outcome == "Bought already" then
		if game.Players.LocalPlayer.Equipped.Value ~= selecter.Value then
			game.ReplicatedStorage.Remotes.EquipTool:FireServer(selecter.Value)
			status.Text = "Equipped"
		end
	
	end
end)
selecter:GetPropertyChangedSignal("Value"):Connect(function()
	local model = selecter.Value
	local toolsbought = game.ReplicatedStorage.Remotes.toolbought:InvokeServer()
	for i, tool in pairs(toolsbought) do
		if tool == model.Name then
			status.Text = "Equipped"
		else
			status.Text = "Equip"
		end
	end
end)

Please help ASAP thank you!

Which code line does the error points to?

line 58 in server side which is:

elseif price > player.leaderstats.Coins.Value and not game.ServerStorage.OwnedItems[player.Name]:FindFirstChild(item) then

line 6 in the client side which is:

local outcome = rep.Remotes.sale:InvokeServer(selecter.Value)

This just means that the folder containing the player’s name is not in ReplicatedStorage. Make sure it’s in there.

This is the culprit: Screenshot_2021-11-18-12-36-08-84_40deb401b9ffe8e1df2f1cc5ba480b12

The error means that something that you’re looking for is not there. In your case, when doing Owneditems[player.Name], it looks for a child named player.Name inside of OwnedItems. Except its not there, and that’s why it errors.

How do i make the player’s name in present in the replicatedStorage? Also thank you for the clarification.

How do i make the player’s name in present in the replicatedStorage? Also thank you for the clarification.

It sounds like you just copied that code out of nowhere and expect it to work.

Please make sure the logic of presenting the player’s name in ReplicatedStorage is working as intended if you made it.

fyi i didnt copy the code , i actually wrote the code by myself. With no help, i got alot errors and i fixed all of them except this error.

You put it there yourself. You can use a StringValue to accomplish that.

Try game.Players instead of game.ReplicatedStorage. Cuz u parented OwnedItems folder to player.

local items = Instance.new("Folder", player) -- player is in game.Players
	items.Name = "OwnedItems"

Hey everyone, I Fixed my error, thank you for helping me. What i did was i made a variable and set it’s value as the player’s name. Now it works.

3 Likes

Make sure to set your comment to a solution as you have identified the problem.

2 Likes

oops sorry i forgot lol

(bruh moment)

1 Like