Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Daily reward system. Money duplicating instead of adding?

Asked by 3 years ago

Ok, so im a decent scripter but for the life of me I can't see my mistake here. I'm only posting part of this script. The entire script runs! The script is a daily reward system when clicking a chest on the map.

Problem: the first time collecting runs smooth, second time running increases your money by thefirstcollection + thesecondcollection. third time does first + second + third, essentially slowly accumulating. aka if day one i gain 50, and i should gain 50 more on day two. its currently collecting both days together for a total of 100.

Can anyone see my mistake here? I'm assuming its how i data saved it but i've tried everything i can think of

local DataStore = game:GetService("DataStoreService"):GetDataStore("DailyRewards")

local hourWait = 23


script.Parent.Clicker.ClickDetector.MouseClick:Connect(function(player)

    local timeNow = os.time()

    local data

    pcall(function()
        data = DataStore:GetAsync(player.UserId.."Chest01") --Change
        print("Getting Data")
    end)

    if data ~= nil then


        local timeSinceLastClaim = timeNow - data

        print("Time since last claim"..timeSinceLastClaim)

        if (timeSinceLastClaim / 3600) >= hourWait then

            local reward = math.random(50,100)
            game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
            local connection
            connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
                if triggeringPlayer == player then
                    player.leaderboard.Money.Value = player.leaderboard.Money.Value + reward
                    DataStore:SetAsync(player.UserId.."Chest01",os.time()) --Change
                    script.Parent.Clicker.BillboardGui.TextLabel.Text = "You got "..reward.." Money!"
                    wait(2)
                    script.Parent.Clicker.BillboardGui.TextLabel.Text = "Come again in 24 hours"
                    wait(2)
                    script.Parent.Clicker.BillboardGui.TextLabel.Text = ""
                    connection:Disconnect()
                end
            end)
        else
0
Please trim your code. See https://idownvotedbecau.se/TooMuchCode . Also, is this your code? User#30567 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I cant see anything wrong but it sound like the first time you click the chest it gives you the reward and then waits for you to be able to click the chest again. Then when you click it the second time the script runs (as if its the first time you've clicked it) and the script also runs because the wait which begun when you first clicked the chest has ended.

1st Click:
Script 1 runs
Script 1 wait begins

2nd Click:
Script 1 wait ends
Script 1 runs
Script 1 wait begins
Script 2 runs
Script 2 wait begins

3rd Click:
Script 1 wait ends
Script 1 runs
Script 1 wait begins
Script 2 wait ends
Script 2 runs
Script 2 wait begins
Script 3 runs
Script 3 wait begins

...etc.

You could test this by having it print a number when you click the chest and then when all the code is finished saying i = i+1 to see if the next time you click the chest you get 2 or 2 , 1 as seperate outputs (or ever just 1,1 tbh...)

The use of debounce or making sure the script isn't both listening and waiting for you to click at the same time might help?

Sorry I can't be of much help :/

0
sorry for the formatting it didnt look like that at the time of typing.. AlexTheCreator 461 — 3y
Ad

Answer this question