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:

1datastore: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:

1local data = game:GetService("DataStoreService"):GetDataStore("Test")
2data:SetAsync("Key", "Hello there") -- Assign a value
3 
4wait(10) -- Keep the value for 10 seconds
5 
6data: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.

01local sampleDataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
02 
03game.Players.PlayerRemoving:Connect(function(player)
04    local playerKey = "Player_" .. player.UserId
05 
06    local success, val = pcall(function()
07        return sampleDataStore:RemoveAsync(playerKey)
08    end)
09 
10    if success then
11        print(val)
12    end
13end)
Ad

Answer this question