So I am currently making a system for when a player clicks a GUI button they purchase an item with their leaderstats money. The way I am doing this is I am firing a remote event. Here is the script for when they click on the GUI.
local purchaseOne = script.Parent.PurchaseOne local item = nil local price = 50 local player = game:GetService("Players").LocalPlayer local rs = game:GetService("ReplicatedStorage") local re = rs:WaitForChild("EquipRe") local function purchaseLevelOne() item = "LevelOne" price = 50 end local function buyIt() local Coins = player.leaderstats:FindFirstChild("Coins") if Coins.Value >= price then Coins.Value = Coins.Value - price re:FireServer(item) end end purchaseOne.Activated:Connect(purchaseLevelOne) purchaseOne.Activated:Connect(buyIt)
After the player clicks on the button it fires the remote event and sends the info over to this next script. This first script works fine and sends all the info correctly. Its just on this second script I get an error ****Argument 1 missing or nil**** on line 7 of this script below.
local rs = game:GetService("ReplicatedStorage") local re = rs:WaitForChild("EquipRe") local function giveTool(player, item) print("Test") local tools = rs:WaitForChild("tools") local tool = tools:FindFirstChild(item):Clone() tool.Parent = player.Backpack end re.OnServerEvent:Connect(giveTool)
I am not sure why it says this because I have a folder within replicated storage named tools with a tool inside.
You are calling both the purchaseLevelOne
and buyIt
functions when the button is activated. To fix the error, I recommend combining both of the functions.
Also, deduct the coins on the server, not the client, as it will not replicate to the server.
The error is talking about the argument to a function (in other words, the (item)
part of tools:FindFirstChild(item)
)
The reason that it's nil is that event connections are triggered in the reverse order they're added. It's better not to ever rely on the event order - instead, create a new function that calls everything in the proper order:
purchaseOne.Activated:Connect(function() purchaseLevelOne() buyIt() end)
Btw, if you're naming variables and functions things like "LevelOne", "LevelTwo", and so on, you should consider looking into tables and for
loops, which can fix unnecessary code duplication.