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

Script Not giving money to the player every 15 seconds?

Asked by 5 years ago

Full script here even tough the only lines that arent working are lines 33 - 36, any ideas why? it isnt even printing done!

DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")
SaveTime = 15 -- Saves every (SaveTime) seconds (I recommend not going under 60 seconds...)

game.Players.PlayerAdded:Connect(function(plr)
    local PlayerKey = "user_".. plr.userId
    local stats = Instance.new("IntValue", plr)
    stats.Name = "leaderstats"
    print(stats.Name)
    if DataStore:GetAsync(PlayerKey)then
        print("Loading " .. PlayerKey)
        local Data = DataStore:GetAsync(PlayerKey)
        local RDCash = Instance.new("IntValue", stats)
        RDCash.Name = "RDCash"
        RDCash.Value = Data.RDCash
    else do -- Setups new data
        local NewStats = {RDCash = 0,}
        local RDCash = Instance.new("IntValue", stats)
        RDCash.Name = "EXP"
        RDCash.Value = 250
        DataStore:SetAsync(PlayerKey,NewStats)
    end
    end
while true do --Save loop
    wait(SaveTime)
    local PlayerKey = "user_".. plr.userId
    local Stats = plr.leaderstats
    local Data = {}
    Data.RDCash = Stats.RDCash.Value
    DataStore:SetAsync(PlayerKey,Data)
    print("Saved Data")
    end

    while true do
    plr.leaderstats.RDCash.Value = plr.leaderstats.RDCash.Value + 15
    print("Done!")
    wait(3)
    end
    end)

1 answer

Log in to vote
0
Answered by 5 years ago

Hi, I have noticed a few mistakes in your code

  1. You have put a while loop, and then put a while loop after it. The first while loop would run infinitely. As a result, the other while loop would not be executed. To fix this, you can add a coroutine. Coroutines are used to make 2 or more things to execute at the same time. In your case, coroutines are relevant. We can simply wrap each of your loops with coroutine.wrap(). In the end, we simply call the two coroutines.

  1. How you use Instance.new may be the cause of problems with performance. I have given an example that creates a part.
local part = Instance.new("Part")  
part.Name = "New Part" 
part.Parent = workspace

As you have noticed in that small piece of code, you use the Instance.new class, then, change the property of that new class, and then, it changes the parent. This decreases latency in your game.


As for 3, there were a few minor mistakes/tweaks that I will not include here. You might notice these changes.


Code:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")
local SaveTime = 15 -- Saves every (SaveTime) seconds (I recommend not going under 60 seconds...)

game.Players.PlayerAdded:Connect(function(plr)
    local PlayerKey = "user_".. plr.userId
    local stats = Instance.new("IntValue")
    stats.Name = "leaderstats"
    stats.Parent = plr
    print(stats.Name)
    if DataStore:GetAsync(PlayerKey) then
        print("Loading " .. PlayerKey)
        local Data = DataStore:GetAsync(PlayerKey)
        local RDCash = Instance.new("IntValue")
        RDCash.Name = "RDCash"
        RDCash.Value = Data.RDCash
    RDCash.Parent = stats
    else -- Setups new data -- I don't know why you put "else do" instead of just putting "else"
        local NewStats = {RDCash = 0}
        local RDCash = Instance.new("IntValue")
        RDCash.Name = "EXP"
        RDCash.Value = 250
    RDCash.Parent = stats
        DataStore:SetAsync(PlayerKey,NewStats)
    end
    local saveData = coroutine.wrap(function()
        while true do --Save loop
            wait(SaveTime)
            local PlayerKey = "user_".. plr.UserId
            local leaderstats = plr.leaderstats
            local Data = {}
            Data.RDCash = leaderstats.RDCash.Value
            DataStore:SetAsync(PlayerKey,Data)
            print("Saved Data")
        end
    end)
    local addCash = coroutine.wrap(function()
         while true do
            plr.leaderstats.RDCash.Value = plr.leaderstats.RDCash.Value + 15
            print("Done!")
            wait(3)
          end
     end)
   saveData()
   addCash()
end)
0
Awesome!!! Youre one of the best scripters ive ever met! SuperBeeperman 30 — 5y
0
Did you understand why your script didn't work and what to do? saSlol2436 716 — 5y
0
Thanks by the way, you know, there are better scripters than me to be honest saSlol2436 716 — 5y
Ad

Answer this question