Why am I getting this error?

I have recently been trying to create a shop, and I am getting this error when the player tries to Buy a weapon.

ServerScript:

player = game.Players.LocalPlayer
dollars = player.leaderstats.Dollars
price = 150
tool = game.Workspace.Guns:FindFirstChild("AK47")

function Buy()
	
if dollars.Value >= price then
		script.Parent.Bought:Play()	
		dollars.Value = dollars.Value - price
		local a = tool:Clone()
		a.Parent = player.Backpack
		local b = tool:Clone()
		b.Parent = player.StarterGear
elseif dollars.Value <= price then
        script.Parent.Failed:Play()	

	end
end
script.Parent.MouseButton1Down:Connect(Buy)

player is nil. is this on a local or server script. (im gonna assume server)

use

dollars = player:WaitForChild("leaderstats").Dollars

the folder hasn’t been created yet when you run this script so it returns nil

check out the screenshot he sent, the error says that “leaderstats” is nil so the script already found the player

the issue isnt the leaderstats folder, its the fact that player is nil because hes trying to use localplayer on the server

1 Like

use a remote event to trigger it to the server in order the value to change on the client and on the server side!

1 Like

in case that’s true, try this script:

game.Players.PlayerAdded:Connect(function(player)
dollars = player:WaitForChild("leaderstats").Dollars
price = 150
tool = game.Workspace.Guns:FindFirstChild("AK47")

function Buy()
	
if dollars.Value >= price then
		script.Parent.Bought:Play()	
		dollars.Value = dollars.Value - price
		local a = tool:Clone()
		a.Parent = player.Backpack
		local b = tool:Clone()
		b.Parent = player.StarterGear
elseif dollars.Value <= price then
        script.Parent.Failed:Play()	

	end
end
script.Parent.MouseButton1Down:Connect(Buy)
end)

sadly, this is going to introduce the problem of using mousebutton1down on the server. (it’s not possible)

he’s gonna have to rewrite this using client and server scripts plus some remote events like @daulric said.

2 Likes

oh, i didn’t check the entire script, you’re right

Thank you! This worked with a bit of modifying.

Also thank you @daulric