I’m currently making a GUI that shows player stats and also a button that resets player stats. I made a script that resets, but I’m using Data Store and it makes the stats not reset if the player leave and rejoin the game. Any help will help me. Thanks.
you cannot change values via client, use a remote event for that.
make a remote event and insert it into ReplicatedStorage,
replace your local script with this:
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
script.Parent.MouseButton1Click:Connect(function()
RemoteEvent:FireServer() -- fires to server once button clicked
end)
then make a Server Script and place it into
ServerScriptService, then insert this into it:
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(plr) -- checks for event, then resets data
plr.leaderstats.Tokens.Value = 0
plr.leaderstats.Kills.Value = 0
plr.leaderstats.Time.Value = 0
end)
If this is the case, you could firstly get the saving script to listen out for the event. Then you could save it, through using the same method, as if they had stats and left the game.
-- Variable
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")
-- Create folders in Local Player
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Tkn= Instance.new("IntValue", Leaderstats)
Tkn.Name = "Tokens"
Tkn.Value = 0
local Kills = Instance.new("IntValue", Leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
local Times = Instance.new("IntValue", Leaderstats)
Times.Name = "Time"
Times.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Tkn.Value = Data.Tokens
Kills.Value = Data.Kills
Times.Value = Data.Times
end
end)
-- save when player is offline
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Tokens"] = Player.leaderstats.Tokens.Value; -- Change "Money" with your currency.
["Kills"] = Player.leaderstats.Kills.Value;
["Times"] = Player.leaderstats.Times.Value;})
end)
-- Save Kills
local Players = game.Players
local Template = Instance.new 'BoolValue'
Players.PlayerAdded:connect(function(Player)
local Stats = Template:Clone()
Stats.Parent = Player
Player.CharacterAdded:connect(function(Character)
local Humanoid = Character:FindFirstChild "Humanoid"
if Humanoid then
Humanoid.Died:connect(function()
for i, Child in pairs(Humanoid:GetChildren()) do
if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
local Killer = Child.Value
if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
local Kills = Killer.leaderstats.Kills
Kills.Value = Kills.Value + 1
end
return
end
end
end)
end
end)
end)
-- Variable
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")
-- Create folders in Local Player
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Tkn= Instance.new("IntValue", Leaderstats)
Tkn.Name = "Tokens"
Tkn.Value = 0
local Kills = Instance.new("IntValue", Leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
local Times = Instance.new("IntValue", Leaderstats)
Times.Name = "Time"
Times.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Tkn.Value = Data.Tokens
Kills.Value = Data.Kills
Times.Value = Data.Times
end
end)
-- save when player is offline
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Tokens"] = Player.leaderstats.Tokens.Value; -- Change "Money" with your currency.
["Kills"] = Player.leaderstats.Kills.Value;
["Times"] = Player.leaderstats.Times.Value;})
end)
-- Save Kills
local Players = game.Players
local Template = Instance.new 'BoolValue'
Players.PlayerAdded:connect(function(Player)
local Stats = Template:Clone()
Stats.Parent = Player
Player.CharacterAdded:connect(function(Character)
local Humanoid = Character:FindFirstChild "Humanoid"
if Humanoid then
Humanoid.Died:connect(function()
for i, Child in pairs(Humanoid:GetChildren()) do
if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
local Killer = Child.Value
if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
local Kills = Killer.leaderstats.Kills
Kills.Value = Kills.Value + 1
end
return
end
end
end)
end
end)
end)