i just made a datastores for my game yesterday and wanted to know how can i make a “new game” button so bassicly once the player beats the game it will delete there datastores and they can play the game again
if it deleted there datastore then when they play the game again will it not save there progress?
datastore script
-- local DataSoreService = game:GetService("DataStoreService")
local myDataStore = DataSoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local collectibles = Instance.new("IntValue")
collectibles.Name = "collectibles"
collectibles.Parent = leaderstats
local data
local succes, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-collectibles")
end)
if succes then
collectibles.Value = data
else
print("there was an error")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
myDataStore:SetAsync(player.UserId.."-collectibles",player.leaderstats.collectibles.Value)
end)
if succes then print("data saved")
else
print("there was an error")
warn(errormessage)
end
end)
Just like @0hJudas said,
just changing the value of the player’s collectibles on the serverside to 0 would automatically save this value whenever the player leaves the game, so you don’t really have to tamper with the datastore itself
soo, you now just have to change the player’s collectibles value to 0 and then kick them, and their datastore values will become as good as new
let me give more context
i am making a 3d plat former game kinda like mario odessey and the way im separating the different worlds is im using different “places” like games and there are teleporters in each world to teleport (the way you open the portal is to get 5 collectibles or “stars”) you to the next so bassicly each world is a whole new game
the last world doest have any datastores since its just a one time bossfight once you beat the boss the ui to restart the game will apear how would i make the button kick the player and also delete the datastores in the other worlds
If am getting you right, you’re using multiple places under the same game, soo you want to reset the values after the player finishes the game at the final place.
So now am assuming after the player finishes the boss level you’ll be teleporting them to the starter place, so now you have to change the player’s value to 0 and the use the set async function separately before teleporting them.
pcall(function()
myDataStore:SetAsync(player.UserId.."-collectibles",player.leaderstats.collectibles.Value)
end)
if succes then print("data saved")
else
print("there was an error")
warn(errormessage)
end
You probably would have to run this under serverscriptservice through a remote event after the particular player completes the game
local function OnRemoteFired(Player)
local Success, Result = pcall(function() return DataStore:RemoveAsync(Player.UserId) end) --Wipe data.
if not Success then warn(Result) end --Warn if necessary.
Player:Kick() --Kick player.
end
You’d just connect a function akin to the above to a RemoteEvent’s ‘OnServerEvent’ signal (on the server of course).
i just found out that the data stores save from all the different places which is good if i want to delete them but its not good since if you need 5 collectibles to beat the first world then once you get to the second then you would already have beat it.