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

Why isn't DataStore not loading nor saving the values it contained?

Asked by 4 years ago

So basically I've made a DataStore containing 4 values but it didn't load nor save those 4 values it contained and I swear the last time I've made a DataStore containing more than 1 values worked! But this time it didn't! Please check my script.

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

game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder",Player)
    Leaderstats.Name = "Leaderstats"

    local Coins = Instance.new("IntValue",Leaderstats)
    Coins.Name = "Coins"

    local Written = Instance.new("IntValue",Leaderstats)
    Written.Name = "Written"

    local Gems = Instance.new("IntValue",Leaderstats)
    Gems.Name = "Gems"

    local Restarts = Instance.new("IntValue",Leaderstats)
    Restarts.Name = "Restarts"

    local LoadedCoins = 0
    local LoadedWritten = 0
    local LoadedGems = 0
    local LoadedRestarts = 0

    local Success,Failed = pcall(function()
        LoadedCoins = DataStore:GetAsync(Player.UserId.."Coins")
        LoadedWritten = DataStore:GetAsync(Player.UserId.."Written")
        LoadedGems = DataStore:GetAsync(Player.UserId.."Gems")
        LoadedRestarts = DataStore:GetAsync(Player.UserId.."Restarts")
    end)

    if Success then
        Coins.Value = LoadedCoins
        Written.Value = LoadedWritten
        Gems.Value = LoadedGems
        Restarts.Value = LoadedRestarts
    else
        warn(Failed)
    end

end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Success,Failed = pcall(function()
        DataStore:SetAsync(Player.UserId.."Coins",Player.Leaderstats.Coins.Value)
        DataStore:SetAsync(Player.UserId.."Written",Player.Leaderstats.Written.Value)
        DataStore:SetAsync(Player.UserId.."Gems",Player.Leaderstats.Gems.Value)
        DataStore:SetAsync(Player.UserId.."Restarts",Player.Leaderstats.Restarts.Value)
    end)

    if Success then
        print("Successfully saved "..Player.Name.."'s data!")
    else
        warn(Failed)
    end

end)

0
You should test the script in real-game. DataStore not working properly inside studio. Block_manvn 395 — 4y
0
IT WORKED! THANK YOU VERY MUCH! I PLAYED MY GAME IN ROBLOX PLAYER AND IT WORKED! I thought I have to debug my script! DizzyGuy70 38 — 4y

1 answer

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

If your trying to see if its saving and loading in Roblox Studio, It will only load It can't save in Roblox Studio. Try in a regular server. Or see if you have not turned on API Serivces.

Using Data Stores in Studio DataStoreService cannot be used in Studio if a game is not configured to allow access to API services. See the Data Stores article for instructions.

DataStoreService

0
WOW! THANK YOU SO MUCH!!! WHEN I PLAYED MY GAME IT SAVED ALL THOSE VALUES IT CONTAINED! DizzyGuy70 38 — 4y
0
Your welcome! I also had the same problem like you 3 years ago but now I'm an experienced scripter! You can too, just learn more! NathanTheCraziest 105 — 4y
0
well, I'm happy to help! NathanTheCraziest 105 — 4y
Ad

Answer this question