Change Number Value with a Mouse Click

Hello everyone! So I’ve recently been trying to change a Number value with a Mouse click but its not working and I honestly have no idea why. Here’s the script:

local Players = game:GetService(“Players”);
local Player = Players.LocalPlayer;
local Mouse = Player:GetMouse();
local val = game.ReplicatedStorage.NumberValue.Value

script.Parent.MouseButton1Down:Connect(function()

val = val + 1

end)

Instead of doing it:

Try this:
local val = game.ReplicatedStorage.NumberValue

script.Parent.MouseButton1Down:Connect(function()

val.Value += 1

end)

I’m not sure how efficient is it but you could try.

Yeah i just changed the .Value place. Sometimes, it could cause to not work

The reason this is happening is because when you access the value and store it in a variable the value stored at that address will remain the same.
So when you are storing the value and if the value is 0 every time you increment the value 1 will be added to 0 and thus it will not get incremented to the current value but the value stored in that variable which is 0, resulting in 0 + 1 = 1.
In that case access the value the moment you are changing it. In the function scope.
The solution provided by @Boustifail would work.

1 Like

Thank you! Do you know how I can turn the value of the NumberValue into a string?

Use the tostring() function.
Takes one argument.
Pass the argument you want to turn, into a string.

@Zohair_028 has the solution :wink: