Leaderstats broken

The Money line doesnt work im getting an error in the output.

  --Config


local player = game.Players:findFirstChild()



local price = 15000  --Change to price of vehicle
local CarName = "Beige Dune Buggy" --Change to vehicle name


--Do not edit past this

local player = script.Parent.Parent.Parent.Parent.Parent
local Money = player.leaderstats.Money 


script.Parent.Parent.Parent.Name = CarName
local BuyTitle = script.Parent.Parent.TextLabel
BuyTitle.Text = "Would you like to buy " .. CarName .. " for " .. price --This automaticly changes the GUI to show the name of the vehicle and the price!


script.Parent.MouseButton1Click:Connect(function()
		if Money.Value >= price and player[CarName][CarName].Value == 0 then--If the player has enough money and does not own the vehicle
		Money.Value = Money.Value - price--Takes the money

		player[CarName][CarName].Value = 2
		wait()

		script.Parent.Parent.Parent.Enabled = false
	elseif Money.Value <= price and player[CarName][CarName].Value == 0 then
		BuyTitle.TextColor3 = Color3.new(1, 0, 0.0156863)
		wait(0.3)
		BuyTitle.TextColor3 = Color3.fromRGB(150, 150, 150)

		--BuyTitle.TextColor3 = Color3.new(150, 150, 150)

	end

end)

What is the error that you get in output?

also, where is this code, and what are you trying to accomplish with it?

leaderstats is not a valid member of Folder “Workspace.Game” - Server - PurchaseScript:10

This doesnt work for a reason.

It is because your player value is nil, nil basically means it was not assigned, and that is what is causing the error. That leads me to believe that the script is not located in a place where the player is script.Parent.Parent.Parent.Parent.Parent

Figured it out! The script is located in the workspace meaning you cannot get the player by using script.Parent.Parent.Parent.Parent.Parent

local player = game.Players:FindFirstChild() needs to require an argument, for example; local player = game.Players:FindFirstChild("IAmTailerr")
Also make sure you capitalize the f on Find in your code.
If this is running in a localscript you can use local player = game.Players.LocalPlayer, however editing money from a local side shouldn’t be done and should be done server side.

Its in starterGUI not workspace

Can you show me exactly where?

Capture
its here in starter GUI

First things first I would HIGHLY recommend transferring that script into a local script because then you can just state, local player = game.Players.LocalPlayer

Exactly this, and then using a RemoteEvent to do messages between the server to remove their money, otherwise the server won’t see this, leaderstats won’t update server-side and datasaving wont work.

Yes, the rewritten script would be:
Local Script:

local player = game.Players.LocalPlayer
local gui = script.Parent.Parent.Parent

local price = 15000  --Change to price of vehicle
local CarName = "Beige Dune Buggy" --Change to vehicle name

local Money = player:WaitForChild("leaderstats").Money


gui.Name = CarName
local BuyTitle = script.Parent.Parent.TextLabel
BuyTitle.Text = "Would you like to buy " .. CarName .. " for " .. price --This automaticly changes the GUI to show the name of the vehicle and the price!


script.Parent.MouseButton1Click:Connect(function()
		if Money.Value >= price and player[CarName][CarName].Value == 0 then--If the player has enough money and does not own the vehicle
        game.ReplicatedStorage.RemoteEvent:FireServer(price, CarName)
		wait()

		script.Parent.Parent.Parent.Enabled = false
	elseif Money.Value <= price and player[CarName][CarName].Value == 0 then
		BuyTitle.TextColor3 = Color3.new(1, 0, 0.0156863)
		wait(0.3)
		BuyTitle.TextColor3 = Color3.fromRGB(150, 150, 150)

		--BuyTitle.TextColor3 = Color3.new(150, 150, 150)

	end

end)

ServerScript:


game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(price, CarName, player)
     player.leaderstats.Money.Value = player.leaderstats.Money.Value - price
     player[CarName][CarName].Value = 2
end)

Also make sure you have a remote event in replicated storage, called “RemoteEvent” :smiley:

Not only that, but it when somebody purchases an item, it will purchase for the entire server. @dav2777 you should definitely be using a LocalScript in this situation.

Yeah, also that… Never use server scripts in client sided scenarios

1 Like

what is this part that you added?

Im getting this error on ser script:

ServerScriptService.CarPurchase:2: attempt to index nil with ‘Money’ - Server - CarPurchase:2

Make a script in serverscript service and make sure you have a remote event in replicated storage, called “RemoteEvent”

its error because the its findFirstChild and not FindFirstChild, the one at local player