I’m making a rebirth system for my simulator game! I’m currently having a bug, the bug is that when I’m setting the player’s leaderstat (coins) to 0, it sets to 0 but when I click again on the clicker button, it comes back to how much it was before. Let’s say if it was 100K money before, I rebirth and it sets to 0 then I click again it goes up to 100k again in 1 click. How can I fix this?
Here’s the script:
local Player = game.Players.LocalPlayer
local Rebirths = Player.leaderstats.Rebirths
local CoinsNeeded = 100 * Rebirths.Value
local Coins = Player.leaderstats.Coins
if Rebirths == 0 then
CoinsNeeded = 100 * 1
end
script.Parent["1R"].Text = CoinsNeeded
script.Parent["1R"].MouseButton1Click:Connect(function()
script.Parent["1R"].Text = CoinsNeeded
if Player.leaderstats.Coins.Value >= CoinsNeeded then
Player.leaderstats.Rebirths.Value = Player.leaderstats.Rebirths.Value + 1
Coins.Value = 0
else
print("Not Enough Coins")
end
end)
It appears that the problem is that after the player hits the button, you are not changing the value of CoinsNeeded. CoinsNeeded is currently only set once after script execution and is never modified again.
You can simply reposition the line CoinsNeeded = 100 * Rebirths to correct this.
To be updated each time the player clicks the button, place the value within the MouseButton1Click event.
The revamped/fixed script would appear as follows:
local Player = game.Players.LocalPlayer
local Rebirths = Player.leaderstats.Rebirths
local Coins = Player.leaderstats.Coins
script.Parent["1R"].MouseButton1Click:Connect(function()
local CoinsNeeded = 100 * Rebirths.Value
if Rebirths.Value == 0 then
CoinsNeeded = 100
end
script.Parent["1R"].Text = CoinsNeeded
if Player.leaderstats.Coins.Value >= CoinsNeeded then
Player.leaderstats.Rebirths.Value = Player.leaderstats.Rebirths.Value + 1
Coins.Value = 0
else
print("Not Enough Coins")
end
end)
By doing this, the value of Rebirths will be used to recalculate CoinsNeeded each time the player presses the button. This ought to resolve the problem with the player’s coins reverting to their original value.
Well that did just solve the “not enough coins” system, It didn’t solve anything with the leaderstats. The thing I want to reach is that resets the leaderstats. Now it does not reset datastore, only the stats
I was a little confused because I assumed that was what you wanted, but in any case, perhaps this will help:
local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("Coins")
coinsDataStore:SetAsync(Player.UserId, 0)
The player’s coin count will be reset to 0 in the data storage as a result. Other leaderstats in the data store can be reset in a similar manner.
Also, I do not know what your data store is called.
local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("Coins")
Put this part at the top of the script, like this:
local Player = game.Players.LocalPlayer
local Rebirths = Player.leaderstats.Rebirths
local Coins = Player.leaderstats.Coins
local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("Coins")
I never noticed the Player part, but anyways to reset the DataStore, you’ll probably have to solve that. I thought it was in a server script, as I never noticed the localPlayer part
It’s happening because you’re recomputing CoinsNeeded with the old Rebirths.Value after you’ve increased it. You should set CoinsNeeded in the if: if Rebirths == 0 then
CoinsNeeded = 100 * 1
end