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

How do i reset a player's stats when they press "New game"?

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)

3 comments

0

Maybe set all Values to 0 and then possibly save them

0

Can we get an example of the code used to save the data? This will help.

0

Ok, edited

2 answers

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"

8 comments

0

its saying Expected '}' (to close '{' at line 43. got defense so i put ; after each line and that got rid of the red lines. Then when i tested it the stats did not save

0

ah, i forgot commas. Please accept my answer :)

0

The stats dont save tho

0

Uh, that's your problem then, maybe because PlayerRemoving? My code is fine though... Any errors?

0

no errors

0

Can you add me on discord? Sulfonic#2593

0

Get rid of DataStoreVersion

0

looks like there also needs to be an end) at line 41

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