I basically want to create a button "New Game" and when they press it their stats get reset. I have the gui and in the gui i have the Script That fires an event named "ResetData" but i dont know what to put in the server script that makes the remote event reset the stats
DataStore
local DataStoreService = game:GetService("DataStoreService") local DataStoreVersion = 10 -- Changing this version will wipe all data of players local DataStore = DataStoreService:GetDataStore("DataStorex"..DataStoreVersion) -- Changing "DataStoreV" will also wipe all data of players game.Players.PlayerAdded:Connect(function(Player) local Stats = Instance.new("Folder",Player) Stats.Name = "Stats" local Strength = Instance.new("IntValue",Stats) Strength.Name = "Strength" local Defense = Instance.new("IntValue",Stats) Defense.Name = "Defense" local Energy = Instance.new("IntValue",Stats) Energy.Name = "Energy" local Speed = Instance.new("IntValue",Stats) Speed.Name = "Speed" local Rebirths = Instance.new("IntValue",Stats) Rebirths.Name = "Rebirths" local loadedstrength local loadeddefense local loadedenergy local loadedspeed local loadedrebirths local success,errormessage = pcall(function() loadedstrength = DataStore:GetAsync(Player.UserId.."-Strength") loadeddefense = DataStore:GetAsync(Player.UserId.."-Defense") loadedrebirths = DataStore:GetAsync(Player.UserId.."-Energy") loadedrebirths = DataStore:GetAsync(Player.UserId.."-Speed") loadedrebirths = DataStore:GetAsync(Player.UserId.."-Rebirths") end) if success then Strength.Value = loadedstrength Defense.Value = loadeddefense Energy.Value = loadedenergy Speed.Value = loadedspeed Rebirths.Value = loadedrebirths else print("Data load encountered an error,Player Data not loaded") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(Player) local success,errormesssage = pcall(function() DataStore:SetAsync(Player.UserId.."-Strength",Player.Stats.Strength.Value) DataStore:SetAsync(Player.UserId.."-Defense",Player.Stats.Defense.Value) DataStore:SetAsync(Player.UserId.."-Energy",Player.Stats.Energy.Value) DataStore:SetAsync(Player.UserId.."-Speed",Player.Stats.Speed.Value) DataStore:SetAsync(Player.UserId.."-Rebirths",Player.Stats.Rebirths.Value) end) if success then print("Data Save was successful") else print("Data Save Error Occured, Data not saved") warn(errormesssage) end end)
Maybe set all Values to 0 and then possibly save them
Ok i tried it but it doesnt save and it doesnt change energy and speed to 0 for some reason
game.ReplicatedStorage.ResetData.OnServerEvent:Connect(function(clicked) clicked.Stats.Strength.Value = 0 clicked.Stats.Defense.Value = 0 clicked.Stats.Energy.Value = 0 clicked.Stats.Speed.Value = 0 clicked.Stats.Rebirths.Value = 0 end)
Use a table. It's so much cleaner compared to using different keys. I've fixed your code:
local DataStoreService = game:GetService("DataStoreService") local DataStoreVersion = 10 -- Changing this version will wipe all data of players local DataStore = DataStoreService:GetDataStore("DataStorex"..DataStoreVersion) -- Changing "DataStoreV" will also wipe all data of players game.Players.PlayerAdded:Connect(function(Player) local Stats = Instance.new("Folder",Player) Stats.Name = "Stats" local Strength = Instance.new("IntValue",Stats) Strength.Name = "Strength" local Defense = Instance.new("IntValue",Stats) Defense.Name = "Defense" local Energy = Instance.new("IntValue",Stats) Energy.Name = "Energy" local Speed = Instance.new("IntValue",Stats) Speed.Name = "Speed" local Rebirths = Instance.new("IntValue",Stats) Rebirths.Name = "Rebirths" local loadedstrength local loadeddefense local loadedenergy local loadedspeed local loadedrebirths local DataStoreReturned = DataStore:GetAsync(Player.UserId) loadedstrength = DataStoreReturned.Strength loadeddefense = DataStoreReturned.Defense loadedenergy = DataStoreReturned.Energy loadedspeed = DataStoreReturned.Speed loadedrebirths = DataStoreReturned.Rebirths Strength.Value = loadedstrength Defense.Value = loadeddefense Energy.Value = loadedenergy Speed.Value = loadedspeed Rebirths.Value = loadedrebirths game.Players.PlayerRemoving:connect(function(player) DataStore:SetAsync(plyr.UserId,{ Strength = Player.Stats.Strength.Value Defense = Player.Stats.Defense.Value Energy = Player.Stats.Energy.Value Speed = Player.Stats.Speed.Value Rebirths = Player.Stats.Rebirths.Value }) end)
Also, since you said you were having trouble with tables, heres a very simple example:
local value={ TableValue1 = "test" }
if you type value.TableValue1 it will be "test"