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

i made a datastore script but the script didn't save any thing ??

Asked by 4 years ago

this script is not saving any thing in real game and studio i enabled the api access and not working any one can help me

Here Is The Script:-

local currencyName = "Cash"

local expneededName = "ExpNeeded"

local expName = "Exp"

local levelName = "Level"

local breathingName = "HaveBreathing"

local birdName = "HaveBird"

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

game.Players.PlayerAdded:Connect(function(player)

    local folder = Instance.new("Folder")
    folder.Name = "stats"
    folder.Parent = player  

    local currency = Instance.new("IntValue")
    currency.Name = currencyName
    currency.Parent = folder

    local expneeded = Instance.new("IntValue")
    expneeded.Name = expneededName
    expneeded.Parent = folder

    local exp = Instance.new("IntValue")
    exp.Name = expName
    exp.Parent = folder

    local level = Instance.new("IntValue")
    level.Name = levelName
    level.Parent = folder

    local havebird = Instance.new("IntValue")
    havebird.Name = birdName
    havebird.Parent = folder

    local havebreathing = Instance.new("IntValue")
    havebreathing.Name = breathingName
    havebreathing.Parent = folder

    local ID = currencyName.."-"..player.UserId
    local ID5 = expneededName.."-"..player.UserId
    local ID4 = expName.."-"..player.UserId
    local ID3 = levelName.."-"..player.UserId
    local ID2 = breathingName.."-"..player.UserId
    local ID1 = birdName.."-"..player.UserId

    local savedData = nil   

    pcall(function()
        savedData = DataStore:GetAsync(ID)
        savedData = DataStore:GetAsync(ID5)
        savedData = DataStore:GetAsync(ID4)
        savedData = DataStore:GetAsync(ID3)
        savedData = DataStore:GetAsync(ID2)
        savedData = DataStore:GetAsync(ID1)
    end)

    if savedData ~= nil then
        currency.Value = savedData
        level.Value = savedData
        exp.Value = savedData
        expneeded.Value = savedData + 20
        havebird.Value = savedData
        havebreathing.Value = savedData
        print("Data loaded")
    else
        -- New player
        currency.Value = 0
        level.Value = 0
        expneeded.Value = 20
        exp.Value = 0
        havebreathing.Value = 0
        havebird.Value = 0
        print("New player to the game")
    end

exp.Changed:connect(function()
     if player.stats:WaitForChild("Exp").Value >= player.stats:WaitForChild("ExpNeeded").Value then
      level.Value = level.Value + 1
      expneeded.Value = math.floor(expneeded.Value * 3) --You can change this if you want.
      exp.Value = 0 
    end
end)

game.ServerStorage.GiveExp.Event:Connect(function(amount,plr)
  if player.Name == plr.Name then
   exp.Value = exp.Value + amount
        end
    end)
end)
game.Players.PlayerRemoving:Connect(function(player)
    local ID = currencyName.."-"..player.UserId
    local ID5 = expneededName.."-"..player.UserId
    local ID4 = expName.."-"..player.UserId
    local ID3 = levelName.."-"..player.UserId
    local ID2 = breathingName.."-"..player.UserId
    local ID1 = birdName.."-"..player.UserId
    DataStore:SetAsync(ID1,player.stats[breathingName].Value)
    DataStore:SetAsync(ID2,player.stats[birdName].Value)
    DataStore:SetAsync(ID3,player.stats[expneededName].Value)
    DataStore:SetAsync(ID4,player.stats[expName].Value)
    DataStore:SetAsync(ID5,player.stats[levelName].Value)
    DataStore:SetAsync(ID,player.stats[currencyName].Value)
end)

game:BindToClose(function()

    -- When game is ready to shutdown

    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick("This game is shutting down")
        end
    end

    wait(5) 

end)

0
Any errors? nothesducks 15 — 4y
0
not working and no errors in output TheGreenGaming886 2 — 4y
0
The only thing that your pcall should get is birdName because you do savedData = .... and constantly replace the things you get blowup999 659 — 4y
0
trying adding other variables local currency, currency = DataStore:GetAsync(ID) instead of savedData blowup999 659 — 4y
View all comments (2 more)
1
One question. Do you change those values using local scripts? Because local script changes values on the CLIENT, and the SERVER CAN'T see changes made by the client. One more tip is to get all of these values in a table, use http service to encode the table then decode it. If you would like to learn how to do that, just contact me. User#32819 0 — 4y
0
my eyes are popping TNTIsLyfe 152 — 4y

Answer this question