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

DataStore not Saving Data?

Asked by 3 years ago

Hello

I wanted to make a DataStore script for my game (Script in ServerScriptService) and I tried using that and it doesn't work, can someone explain me why? The script including 49 lines.

Here is the script:

ocal DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats

    local playerUserId = "Player_"..player.UserId


    local data
    local success, errormessage = pcall(function()
         data = myDataStore:GetAsync(playerUserId)
    end)



    if success then
        Coins.Value = data
    end



end)


game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local data = player.leaderstats.Coins.Value

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

    if success then
        print("Data successfully saved!")
    else
        print("There was an error!")
        warn(errormessage)
    end

end)
0
make sure to but an if data then because it will errrr Rayguyban2 89 — 3y
0
On the first line it says "ocal" instead of "local" and idk if that is a problem but just revise your script Shounak123 461 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

This often happens because API services are not enabled in roblox studio. Click "Game Settings" in the top bar and select "Permissions". There you will see an option to enable roblox API services. Enable the option and test the game again. It will be working fine.

0
You're correct, But I already did it and it DIdn't work before you answered, But thanks for trying to help me! RektwayYTB 123 — 3y
Ad

Answer this question