I want to make a system where the money you get per click is added by one every time the item is bought however if you buy it once than it adds it like normal but then suddenly makes it like 4 and multiplying by 2 every click? how can I prevent this from happening?
Here’s the script.
local amount = 1
script.Parent.MouseButton1Click:Connect(function()
wait(.1)
game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value + amount
if game.Players.LocalPlayer.Backpack:FindFirstChild("Grey Cube") then
amount = amount + 1
end
end)
I have already tried brainstorming solutions but to no avail.
First of all, what is the wait(.1) for?
Secondly, this script can be exploited very easily since an exploiter can edit the amount to a huge number to instantly get alot of money. However, the money won’t even update on the server currently, since you are setting it using a local script.
I am not sure what you are asking, but I can tell you a small tip to make your scripts less cluttered:
So im still new but I think the .1 helps prevent auto clickers and for the second one its just a test so im not so worried about exploiters
What im trying to do is when you click a button it gives a one dollar and when you buy the grey cube thing it adds by one and only one but what its doing is adding more than once per click
the reason why this is happening is that you keep adding to the amount so basically you are adding to the amount and then adding to the player stat value.
you should do
Instead of simply doing wait(.1), you would have to add a debounce since right now the script would just run 0.1 second late, not actually slowing it.
To make a debounce:
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
if debounce == false then
debounce = true
-- your script here
-- in the end after script ends,
wait(0.1) -- you can also use task.wait(0.1), which should be more accurate
debounce = false
end
end)
Basically, what a debounce does is prevents something from running.
For now, try to apply what I told you, and see if it still does this, since it could have been a problem with the click cooldown
Edit: Do you want the amount of money to always increase or not?
Example:
Click 1: +1 Money
Click 2: +2 Money
Click 3: +3 Money
Click 4: +4 Money
How does the cube purchase work exactly? Maybe you could count the amount of cubes in the inventory and increase by that amount.
Also, I recommend doing all this on the server, including the amount.
All you would need from the client is sending the event that the player clicked, and calculate everything on the server
The cube is a tool right?
Basically what you could do is first count the amount of cubes in the player backpack:
Note: You might have to edit this a bit since I didn’t write this in studio
local CubesFound = 0 -- used later
for i,Item in Player.Backpack:GetChildren() do -- loops through every item in backpack
if Item:IsA("Tool") and Item.Name == "Grey Cube" then -- if tool and name is "Grey Cube" then
CubesFound += 1 -- increase amount by 1
end
end
Money.Value += CubesFound
You can use something like this.
Also, in this case Attributes might be useful, for example for having different amount of cash increase for different cube types.