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

How can I create a shop GUI?

I am creating a mining game, and need to script a text button so that it adds the amount of dirt in the player's inventory to their money, and then removes all of the dirt. Here is the code I am using (in a LocalScript inside a TextButton):

local players = game:GetService("Players")
local player = players.LocalPlayer
local lstats = player:FindFirstChild("leaderstats")
local pstats = player:FindFirstChild("playerstats")
local money = lstats.Money.Value
local dirt = pstats.Dirt.Value
 
script.Parent.MouseButton1Down:connect(function(onClicked)
    money = money + dirt
    dirt = 0
end)

Leaderstats and playerstats are both folders that are generated as children of the player when they first load into the game. When I go into studio and click the button, it does exactly nothing. Is there a way in which I can fix this? Any help is appreciated.

Thank you!

1 comment

0

If you do want to make a cool shop gui though, I recommend reading this: https://developer.roblox.com/articles/GUI-Animations and also the related articles you can find below it.

1 answer

✓ Accepted answer

Your title is a little misleading, it should be a once sentence explanation of your issue, in question format.

That aside...

Values and references

You are assuming that money and dirt are references to the Value property. This is not the case. When the Value property changes, money or dirt will not change, and if you change money or dirt, the Value will not update accordingly.

What you must do is just make money and dirt refer to their IntValue/NumberValue object directly, and directly access the Value property.

References

It is important to know the difference between a value and a reference.

You can think of references as holding some memory location.

In Lua, for example, functions are passed by reference.

Here is some code to demonstrate this:

local fn1, fn2, fn1ref;

fn1 = function()
	for i = 1, 12 do
		print(i^(1/3));
	end
end

fn2 = function()
	for i = 1, 12 do
		print(i^(1/3));
	end
end

fn1ref = fn1;
print(fn1 == fn1ref, rawequal(fn1, fn1ref)); --> true	true
print(fn1 == fn2, rawequal(fn1, fn2)); --> false	false

fn1 and fn1ref contain the same exact function in memory. fn2 is different despite the code, line count, ect, being the same.

Values

Datatypes such as booleans and strings for example, are passed by value, and here is some code to demonstrate how this works:

local bool = false;
local bool2 = bool;
bool = true;
print(bool, bool2); --> true	false

The variables have different memory allocation, and thus the modification of bool did not modify bool2.

Applying this knowledge into your code, and more

onClicked looks like a good variable name to use for a function, yet this is not the case. onClicked is actually the 2D X axis of your mouse. The arguments passed to your MouseButton1Down listener are actually the 2D X and Y axes. This is misleading. You aren't even using onClicked.

Let's clean that portion up.

It might also be a good idea to handle this server side. Doing this client side means it will not replicate.

On the server side, you want to create a RemoteEvent, name it something like addMoney, place it in ReplicatedStorage, so the server and client can see it. Remotes allow for client <-> server communication. If you do not understand remote events, I gave an explanation on RemoteEvents here.

local remoteEvent = Instance.new("RemoteEvent");
remoteEvent.Name = "addMoney"; --// name whatever you want
remoteEvent.Parent = game:GetService("ReplicatedStorage");

remoteEvent.OnServerEvent:Connect(function(client, ...)
	local money, dirt = client.leaderstats.Money, client.playerstats.Dirt;
	money.Value = money.Value + dirt.Value;
    dirt.Value = 0;
end);
--// modify to needs

And on the client side, you simply call :FireServer:


--// consider using activated instead, it works not only for computers but all devices, and :connect is deprecated, use :Connect
local ReplicatedStorage = game:GetService("ReplicatedStorage");

button.Activated:Connect(function()
	ReplicatedStorage.addMoney:FireServer();
end);

And that is it! It works server side now.

1 comment

0

This worked perfectly! Thank you for your advice!

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