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!
Your title is a little misleading, it should be a once sentence explanation of your issue, in question format.
That aside...
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.
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:
```lua 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.
Datatypes such as boolean
s and strings
for example, are passed by value, and here is some code to demonstrate how this works:
lua
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
.
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 RemoteEvent
s here.
```lua
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
:
```lua
--// 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.