Button is not giving me cash upon clicking it

My script isn’t working, sorry for the uninformative title, but why isn’t this working, when you hit the SurfaceGui Button it should give you cash, but it doesn’t

game.Workspace.BasicDice.SurfaceGui.TextButton.MouseButton1Click:Connect(function(player)
local Cash = player.leaderstats.Money
Cash.Value = Cash.Value + 1000
end)
game.Workspace.BasicDice.SurfaceGui.TextButton.MouseButton1Click:Connect(function(player)
    local Cash = player:FindFirstChild(leaderstats).Money
    Cash.Value = Cash.Value + 1000
end)

Is this script on the client or on the server?

could be both it works either way

Nope, if it’s on the client it won’t replicate to the server, meaning that it’s essentially useless. That 1000 cash will only be seen by the local player on that server

1 Like

im very confident it does work try it

I’m on a ServerScript, I tried this and I get
ServerScriptService.Script:24: attempt to index nil with 'FindFirstChild'

Local scripts only work for the client (this is with filtering enabled on). Changes made from the client on the server do not show for any other players, or the server itself.

fixed

  local Cash = player:FindFirstChild("leaderstats").Money
    Cash.Value = Cash.Value + 1000

Hello Clondus,

I think as soon as you mentioned surfacegui, i believe i know what is wrong with it. When you’re making clickable buttons with a surface gui, you have to make sure that the surface gui is inside your playergui and adornee’d to the part that it’s currently on, this means that you can click on the button.

Also, please do make sure that you consider moving your money transfers over to server side as newer tycoons won’t be able to acknowledge that you actually received 1000 money. For this, you would have to make a remote event.

Hope this fixes your issue!

oh boy, i forgot those “” lol.

a server script can detect the button click and uses the plr variable form the function to give the money

People, please do your research and testing before posting. OP please also do research and make sure to debug your code and check the console for errors. All of you are still missing a fundamental piece collectively making your responses incorrect: players are not fired as an argument of MouseButton1Click. You need a RemoteEvent that the client can fire upon clicking the button and the server needs to process that request then add currency to the user.

1 Like

Depends if the script is on the client or server side - if it’s on the server side (workspace, serverscriptservice etc) then a remote event isn’t needed

You never should control a Gui from a Script so that’s out of the question entirely. If you want to architecture your game properly, LocalScripts should be handling client-side input and the server should be receiving requests from the server to authoritatively change information. So yes you do need a remote if you’re looking to do this properly.

local DataStore = game:GetService("DataStoreService")

local ds = DataStore:GetDataStore("CashSaveSystem")

local part = game.Workspace.Part

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("GiveBasicCash")

game.Players.PlayerAdded:connect(function(player)

local leader = Instance.new("Folder",player)

leader.Name = "leaderstats"

local Cash = Instance.new("IntValue",leader)

Cash.Name = "Money"

Cash.Value = ds:GetAsync(player.UserId) or 5000

ds:SetAsync(player.UserId, Cash.Value)

while true do

wait(2)

ds:SetAsync(player.UserId, Cash.Value)

end

end)

game.Players.PlayerRemoving:connect(function(player)

ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)

end)

game.Workspace.BasicDice.SurfaceGui.TextButton.MouseButton1Click:Connect(function(plr)

local Cash = plr:FindFirstChild("leaderstats").Money

Cash.Value = Cash.Value + 1000

end)

This is my full script, the leaderstats aren’t showing anymore.

Try using a ClickDetector instead and use this script:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")
local Click = game.Workspace.Part.ClickDetector

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"

	local Cash = Instance.new("IntValue",leader)
	Cash.Name = "Money"

	pcall(function()
		Cash.Value = ds:GetAsync(player.UserId) or 5000
	end)

	spawn(function()
		while true do
			wait(120)
			pcall(function()
				ds:SetAsync(player.UserId, Cash.Value)
			end)
		end
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	pcall(function()
		ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)
	end)
end)

Click.MouseClick:Connect(function(plr)
	local Cash = plr:FindFirstChild("leaderstats").Money
	Cash.Value += 1000
end)