Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I delete a saved datastore in a player?

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?

1 comment

0

Is my answer correct?

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.