Hello everyone, this script somehow still doesn’t save and I really need it to work. Here is what I’ve come up with so far.
local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local Activations = Instance.new("IntValue")
Activations.Name = "Activations"
Activations.Parent = leaderstats
local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats
local Upgrades = Instance.new("Folder")
Upgrades.Name = "Upgrades"
Upgrades.Parent = player
local SpeedLevel = Instance.new("IntValue")
SpeedLevel.Name = "SpeedLevel"
SpeedLevel.Parent = Upgrades
local JumpLevel = Instance.new("IntValue")
JumpLevel.Name = "JumpLevel"
JumpLevel.Parent = Upgrades
-->> Data: Begining
local playerUserId = "Player_"..player.UserId
-->>Data: Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
if data then
Points.Value = data.Points
Wins.Value = data.Wins
Activations.Value = data.Activations
Deaths.Value = data.Deaths
SpeedLevel.Value = data.SpeedLevel
JumpLevel.Value = data.JumpLevel
player.CharacterAppearanceLoaded:Wait()
character:WaitForChild("Humanoid").WalkSpeed = data.WalkSpeed
end
end
end)
-->> Data: Saving Stats
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
Points = player.leaderstats.Points.Value;
Wins = player.leaderstats.Wins.Value;
Activations = player.leaderstats.Activations.Value;
Deaths = player.leaderstats.Deaths.Value;
SpeedLevel = player.Upgrades.SpeedLevel.Value;
JumpLevel = player.Upgrades.JumpLevel.Value;
WalkSpeed = player.Character.Humanoid.WalkSpeed;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data succefully saved!")
else
print("Data saving error")
warn(errormessage)
end
end)