Need help for purchase system with in-game currency

Guys, I’m trying to make a purchase system that if you have enough money for item you will purchase, you purchase and get item that you purchased. But if you dont have enough money, you can’t get item that you will purchase. But I got this error: Workspace.chips.Handle.buyChips:9: attempt to index nil with ‘Currency’
Purchase system code (buyChips script):

local tool = script.Parent.Parent
local click = script.Parent.ClickDetector
local player = game.Players.LocalPlayer
local chat = game.Chat

local cashier = workspace.cashIer.Cashier.Head

local function pickup()
	if player.Currency.Value >= 6 then
		tool.Parent = game.Workspace[player.Name]
		player.Currency.Value = player:WaitForChild("Currency").Value - 6
		chat:Chat(cashier, "u purchased chips 4 noobs!")
	else
		print("lol")
	end
end

click.MouseClick:Connect(pickup)

CurrencyHandler script (in ServerScriptService):

game.Players.PlayerAdded:Connect(function(player)
	local value = Instance.new("NumberValue", player)
	value.Name = "Currency"
end)

Anyone can help?

LocalPlayer isn’t a usable Object for the server as it is restricted to client only, Everything you made looks like it’s all on the server + its missing an optional argument here hence why it sees as a nil

On your pickup() it has one optional parameter that specifies as player when you name it as anything, plyr is basically the same as LocalPlayer but that limits to the player who clicked only

local tool = script.Parent.Parent
local click = script.Parent.ClickDetector
local player = game.Players.LocalPlayer --Server cannot recognize it as it's restricted to LocalScript
local chat = game.Chat

local cashier = workspace.cashIer.Cashier.Head

local function pickup(plyr)
	if plyr.Currency.Value >= 6 then
		tool.Parent = game.Workspace[plyr.Name]
		plyr.Currency.Value = plyr:WaitForChild("Currency").Value - 6
		chat:Chat(cashier, "u purchased chips 4 noobs!")
	else
		print("lol")
	end
end

click.MouseClick:Connect(pickup)
1 Like
local tool = script.Parent.Parent
local click = script.Parent.ClickDetector
local chat = game.Chat

local cashier = workspace.cashIer.Cashier.Head

game.Players.PlayerAdded:Connect(function(player)
local function pickup()
	if player.Currency.Value >= 6 then
		tool.Parent = game.Workspace[player.Name]
		player.Currency.Value = player:WaitForChild("Currency").Value - 6
		chat:Chat(cashier, "u purchased chips 4 noobs!")
	else
		print("lol")
	end
end
end)

click.MouseClick:Connect(pickup)

--Use This As A Script
1 Like

Thanks for help. I’m learning now. So I can do some mistakes sometimes :sweat_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.