Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a player clicks on gui to get money?

When a player clicks on a gui button they get money. Why is this not working? Only the person that clicks need to get the money.

function onClicked()
		local cashmoney = game.ServerStorage.GoldStorage:FindFirstChild(Player.Name)
		if cashmoney then
			cashmoney.Value = cashmoney.Value + 500
		end
end
script.Parent.MouseButton1Down:connect(onClicked)

Thanks

3 answers

✓ Accepted answer

I'd just like to add a few things;


It's a LocalScript (or should be if it's not) so you can take advantage of the LocalPlayer property, which will be the specific Player where the script is running.

local plr = game.Players.LocalPlayer

ServerStorage is on the server, so LocalScripts can't access it. Use ReplicatedStorage instead.

2 comments

1

Bad suggestion. Use RemoteEvents instead.

0

My goal was to identify his problems (that 0_0 didn't already cover) and supply simple and feasible solutions, not to re-write all his code. Coding yourself will always help you learn more than having someone do all the work for you. Even still, in this situation remotes would likely decrease efficiency.

Server Script => ServerScriptService

local Event = game.ReplicatedStorage:FindFirstChild("AddMoney") or  Instance.new("RemoteEvent",game.ReplicatedStorage -- Looks for Event or Creates it.
Event.Name = "AddMoney" -- Names the Event so Client can Find.

Event.OnServerEvent:connect(function(Player) -- Client sends a message through the event, the server receives it, along with the player.
local PlayerMoney  = game.ServerStorage.GoldStorage:FindFirstChild(Player.Name) -- Look for Money Value
        if PlayerMoney then
            PlayerMoney.Value = PlayerMoney.Value + 500 -- Add Money
end

Local Script => In Button

local Event = game.ReplicatedStorage:WaitForChild("AddMoney") -- Look for the Event Created in Server.

script.Parent.MouseButton1Down:connect(function() -- When Button is pressed
Event:FireServer() -- Send message to server.
end

There could be multiple reasons why your script did not work. First, your original script should have been a Local script. If your script was a local script: Your script could not access "ServerStorage" because only the server may access it. If your script was not a local script, your script would not even detect that the button was clicked.

Local Script goes in Button. Server Script goes in ServerScriptService

P.S.: FilteringEnabled Supported.

local Player = script.Parent.Parent.Parent -- Keep taking away/adding .Parent until you reach a reference to the player. You can use prints to help you find this.

function onClicked()
        local cashmoney = game.ServerStorage.GoldStorage:FindFirstChild(Player.Name) -- ^
        if cashmoney then
            cashmoney.Value = cashmoney.Value + 500
        end
end
script.Parent.MouseButton1Down:connect(onClicked)

You were missing a reference to the player. Also make sure this is a regular script as the client wouldn't be able to see anything in ServerStorage.

If this isn't the reason and you just forgot to include the player reference when submitting your question then check to make sure you have an instance named 'GoldStorage' within game.ServerStorage and make 100% sure there's an instance with a name inside there that would match the player's name. Make sure that instance's name is a NumberValue or an IntValue.

All the best.

2 comments

0

But still it doens't work

0

I edited my answer, hope it helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.