01 | game.Players.PlayerAdded:Connect( function (player) |
02 | print (player.Name.. "has joined" ) |
03 | local Cash = player:WaitForChild( "leaderstats" ).Cash.Value |
04 | if Cash then |
05 | print ( "foundCash" ) |
06 | script.Parent.MouseButton 1 Click:Connect( function () |
07 | print ( "ClickedBuy" ) |
08 | local price = script.Parent.Parent.Price.Value |
09 | if Cash > = price then |
10 | print ( ">=" ) |
11 | end |
12 | end ) |
13 | end |
14 | end ) |
If this is a server script, you would have to put it into a local script,
and at the beginning don't put "player" after function because you have game.Players.LocalPlayer
Edit: I just realized another error in your code. When you say:
1 | local price = script.Parent.Parent.Price.Value |
You should have said:
1 | local price = script.Parent.Parent.Price |
And instead of:
1 | if Cash > = price then |
You would do:
1 | if Cash > = price.Value then |
You would do the same thing with the cash variable, and in the end, it would look like this.
01 | local player = game.Players.LocalPlayer |
02 | local Cash = player:WaitForChild( "leaderstats" ).Cash |
03 | if Cash then |
04 | print ( "foundCash" ) |
05 | script.Parent.MouseButton 1 Click:Connect( function () |
06 | print ( "ClickedBuy" ) |
07 | local price = script.Parent.Parent.Price |
08 | if Cash.Value > = price.Value then |
09 | print ( ">=" ) |
10 | end |
11 | end ) |
12 | end |
You can copy and paste if you want.
You have to separate server code from GUI code. The server should (as you have there) be making sure that the player has enough money - but it shouldn't be directly responding to the player pressing a button. Your GUI code should use a RemoteEvent to inform the server of the player's wishes; the server should then confirm that the player can do that.
Resources:
Also note that server scripts don't run when placed in StarterGui - they can be in the workspace or ServerScriptService, see https://developer.roblox.com/api-reference/class/Script