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

How do I delete a saved datastore in a player?

Asked by 5 years ago

I wanna delete a datastore so that it does not interfere with the script.

I tried setting the value to nil:

datastore:SetAsync(Player.UserId, nil)

but it just gave me an error. How would I remove the datastore or set the value to nil?

0
Is my answer correct? JpcPcGamer360 0 — 5y

1 answer

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

Using * DataStoreService*, the only way you can change a value is by overwriting the former. The same thing applies with removing keys or values as well. Just like how you'd "remove" a variable in a script by setting it to * nil*, you'd do the same thing in data store with whatever set method you're using ( SetAsync, > UpdateAsync, etc) - just save a nil value. Here's an example:

local data = game:GetService("DataStoreService"):GetDataStore("Test")
data:SetAsync("Key", "Hello there") -- Assign a value

wait(10) -- Keep the value for 10 seconds

data:SetAsync("Key", nil) -- Essentially removing the value.

This example removes a player’s data store when they leave the game and prints its value at the time of removal.

    local sampleDataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")

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

        local success, val = pcall(function()
            return sampleDataStore:RemoveAsync(playerKey)
        end)

        if success then
            print(val)
        end
    end)
Ad

Answer this question