game.Players.PlayerAdded:Connect(function(player)
print(player.Name.."has joined")
local Cash = player:WaitForChild("leaderstats").Cash.Value
if Cash then
print("foundCash")
script.Parent.MouseButton1Click:Connect(function()
print("ClickedBuy")
local price = script.Parent.Parent.Price.Value
if Cash >= price then
print(">=")
end
end)
end
end)
Why is the output not printing ">=" when I click the textbutton?
2 answers
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:
local price = script.Parent.Parent.Price.Value
You should have said:
local price = script.Parent.Parent.Price
And instead of:
if Cash >= price then
You would do:
if Cash >= price.Value then
You would do the same thing with the cash variable, and in the end, it would look like this.
local player = game.Players.LocalPlayer
local Cash = player:WaitForChild("leaderstats").Cash
if Cash then
print("foundCash")
script.Parent.MouseButton1Click:Connect(function()
print("ClickedBuy")
local price = script.Parent.Parent.Price
if Cash.Value >= price.Value then
print(">=")
end
end)
end
You can copy and paste if you want.
4 comments
no
its not printing>=
Okay, then I don't know what happened.
but is the statement abovr the print value true
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:
- https://scriptinghelpers.org/guides/server-client-relationship-in-roblox
- https://scriptinghelpers.org/guides/how-to-use-remoteevents-properly
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
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
the code won't work if that's in a server script b/c the server doesn't listen to user input and it won't even detect when a new player joins if that code is in a local script
"the server doesn't listen to the user input" there is no user input... If this script is a server script then, it won't work because, the server cannot see any of the players gui's. use a localscript in startergui instead Gun..