Problem: This Gui Wont Let Me Buy Tool With Cash On Leaderstats
Error Message:
local price = 5000 -- Your price goes here.
local tool = game.ServerStorage:WaitForChild("AKS-74U") -- Put the name of your tool here.
local function buy(player)
local money = player.leaderstats:WaitForChild("cash")
if money.Value >= price then
money.Value = money.Value - price
local a = tool:clone()
a.Parent = player.Backpack
end
end
script.Parent.MouseButton1Click:Connect(buy)
When you get the attempt to index nil with âxâ error, that means whatever youâre trying to access does not exist. In this case, âcashâ does not exist. Check to see if âcashâ is parented correctly.
Well not really, âplayerâ is the thing that does not exist. MouseButton1Click does not have any parameters and should be done on the client anyways, where you use LocalPlayer.
Youâll have to reference a LocalScript, a Script, and a RemoteEvent in order to properly accomplish this
In relation to what @ZOMBEIVEroblox said, a RemoteEvent is in simple terms: A way for clients & servers to send data over each other, and vice versa
Youâll have to create a RemoteEvent inside ReplicatedStorage, as thatâs accessible by both the client and server itself
You can use game.Players.LocalPlayer to reference the Player that way instead in the script shown in the OP, the MouseButton1Click Event has no parameters:
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer
local function buy()
Event:FireServer()
end
script.Parent.MouseButton1Click:Connect(buy)
Next, youâd want to create a Script (Preferably) inside ServerScriptService:
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Tool = game.ServerStorage:WaitForChild("AKS-74U")
local Price = 5000
Event.OnServerEvent:Connect(function(Player)
local money = player.leaderstats:WaitForChild("cash")
if money.Value >= price then
money.value -= price
local ToolClone = Tool:Clone()
ToolClone.Parent = Player.Backpack
end
end)
FireServer() would fire a request from the client side, to the server and OnServerEvent would be receiving those said ârequestsâ when theyâre fired
I think @johncena12345678953 made a shop gui that, regardless of the type of script, will not get the player with MouseButton1Click, remote events are the best option.
It would be very good if MouseButton1Click gave the player, everything would be easier, but I think they never will
Thatâs not really good practice though to be referencing so much script.Parent's just to get the Player alone through a Server Script, plus UI Events should be properly handled on the client
Not to mention that sometimes, some Events wonât work if theyâre handled on the server-side
If you have like a bunch more weapons you want to confirm, using RemoteEvents can be an important key aspect
Example, passing a string parameter onto the server:
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer
local function buy()
Event:FireServer("AKS-74U")
end
script.Parent.MouseButton1Click:Connect(buy)
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
Event.OnServerEvent:Connect(function(Player, ToolName)
local Price = 0
local money = player.leaderstats:WaitForChild("cash")
local ToolCheck = game.ServerStorage:FindFirstChild(ToolName)
if ToolCheck then --Checking if there's a Valid Tool inside ServerStorage
if ToolCheck.Name == "AKS-74U" then
Price = 5000
elseif ToolCheck.Name == "OtherToolHere" then
Price = 2500
end
if money.Value >= Price then
money.Value -= Price
local ToolClone = ToolCheck:Clone()
ToolClone.Parent = Player.Backpack
end
end
end)
(I accidentally hit enter, why do you do this to me Forum)