How would i put this:
local PlayerStatsDS = game:GetService("DataStoreService"):GetDataStore("LeaderData_1")
game.Players.PlayerAdded:Connect(function(NP)
local Key = "PDS-".. NP.UserId
local GetSave = PlayerStatsDS:GetAsync(Key)
local PSF = Instance.new("Folder", NP)
PSF.Name = "leaderstats"
local StatsFolder = script.Stats
for _, S in pairs(StatsFolder:GetChildren()) do
local NS = Instance.new(S.ClassName, PSF)
NS.Name = S.Name
NS.Value = S.Value
end
if GetSave then
for n, Stat in pairs(PSF:GetChildren()) do
Stat.Value = GetSave[n]
end
else
local STS = {}
for _, Stat in pairs(StatsFolder:GetChildren()) do
table.insert(STS, Stat.Value)
end
PlayerStatsDS:SetAsync(Key, STS)
end
end)
game.Players.PlayerRemoving:connect(function(OP)
local Key = "PDS-".. OP.UserId
local StatsFolder = OP.leaderstats
local STS = {}
for _, Stat in pairs(StatsFolder:GetChildren()) do
table.insert(STS, Stat.Value)
end
PlayerStatsDS:SetAsync(Key, STS)
end)
into this:
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("Data")
local RS = game.ReplicatedStorage
local leaderstatsChanged = RS.leaderstatsChanged -- remote event
local getLeaderStat = RS.RemoteFunctions.getLeaderStat
local getUpgrade = RS.RemoteFunctions.getUpgrade
local upgradePurchaseClick = RS.RemoteEvents.upgradePurchaseClick
local upgradeChanged = RS.RemoteEvents.upgradeChanged
local keyPart = game.ServerStorage.keyPart.Value
local deleteGem = RS.RemoteEvents.deleteGem
local function saveData(plr)
local data = {
leaderstats = {},
upgrades = {},
GemsCollected = {}
}
for _, stat in pairs(plr.leaderstats:GetChildren()) do
data["leaderstats"][stat.Name] = stat.Value
end
for _, upgrade in pairs(plr.upgrades:GetChildren()) do
data["upgrades"][upgrade.Name] = upgrade.Value -- eg. speed
end
for _, gemName in pairs(plr.GemsCollected:GetChildren()) do
table.insert(data["GemsCollected"], gemName.Name) -- string of the gem name (eg. "2")
end
local success, err = pcall(function()
PlayerData:SetAsync(plr.UserId..keyPart, data)
end)
if success then
warn("--- DATA SAVED ---")
warn(data)
else
warn("--- COULDN'T SAVE DATA ---")
warn("ERR:", err)
end
end
local function getData(plr)
local UserId = plr.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Gems = Instance.new("StringValue")
Gems.Name = "Gems"
Gems.Parent = leaderstats
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Parent = leaderstats
local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats
local upgrades = Instance.new("Folder")
upgrades.Name = "upgrades"
upgrades.Parent = plr
local Speed = Instance.new("IntValue")
Speed.Name = "Speed"
Speed.Parent = upgrades
local Jump = Instance.new("IntValue")
Jump.Name = "Jump"
Jump.Parent = upgrades
local GemsMultiplier = Instance.new("IntValue")
GemsMultiplier.Name = "Gems"
GemsMultiplier.Parent = upgrades
local GemsCollected = Instance.new("Folder")
GemsCollected.Name = "GemsCollected"
GemsCollected.Parent = plr
local data -- array
pcall(function()
data = PlayerData:GetAsync(UserId..keyPart)
end)
if data then
if data["leaderstats"] then
for _,stat in pairs(leaderstats:GetChildren()) do
stat.Value = data["leaderstats"][stat.Name]
end
else
for _,stat in pairs(leaderstats:GetChildren()) do
stat.Value = 0
end
end
if data["upgrades"] then
for _,upgrade in pairs(upgrades:GetChildren()) do
upgrade.Value = data["upgrades"][upgrade.Name] -- eg. speed
end
else
for _,upgrade in pairs(upgrades:GetChildren()) do
upgrade.Value = 0
end
end
if data["GemsCollected"] then
for _, gemName in pairs(data["GemsCollected"]) do -- looping through all the gem names that have already been collected
local GemName = Instance.new("StringValue")
GemName.Name = gemName
GemName.Parent = plr.GemsCollected -- adding collected gem to folder
deleteGem:FireClient(plr, gemName) -- deleting gem for the player because it has already been collected.
end
-- requires no default values
end
else
for _,stat in pairs(leaderstats:GetChildren()) do
stat.Value = 0
end
for _,upgrade in pairs(upgrades:GetChildren()) do
upgrade.Value = 0
end
end
if UserId==2037624852 or UserId==1204864571 then
Gems.Value+=100000
end
plr.leaderstats.Stage:GetPropertyChangedSignal("Value"):Connect(function()
local stage = plr.leaderstats.Stage -- intValue
leaderstatsChanged:FireClient(plr, stage) -- We are telling the player's localscript that the Stage changed.
end)
end
getLeaderStat.OnServerInvoke = function(plr, stat)
return plr.leaderstats[stat].Value
end
getUpgrade.OnServerInvoke = function(plr, upgrade)
return plr.upgrades[upgrade].Value
end
upgradePurchaseClick.OnServerEvent:Connect(function(plr, upgrade)
local price = {
Speed = {25,35,45,70,80},
Jump = {25,35,50},
Gems = {
25, 30, 45, 60, 80, 100,
125, 180, 230, 280, 350, 450
}
}
if plr.upgrades[upgrade].Value > #price[upgrade] then
plr.upgrades[upgrade].Value = #price[upgrade]
elseif plr.upgrades[upgrade].Value < 0 then
plr.upgrades[upgrade].Value = 0
end
--pcall(function()
local balance = plr.leaderstats["Gems"].Value
--print("Gem balance:", balance)
local item_cost = price[upgrade][(plr.upgrades[upgrade].Value)+1]
--print("Item Cost:", item_cost)
local max_upgrades = #price[upgrade]
--print("Max Upgrades:", max_upgrades)
local upgrades = plr.upgrades[upgrade].Value
--print("Upgrades:", upgrades)
if item_cost and balance >= item_cost then -- Checking if player has enough Gems to buy the upgrade
if upgrades < max_upgrades then -- eg if player has 4/5 they can still buy 1 more upgrade
plr.leaderstats.Gems.Value -= item_cost
plr.upgrades[upgrade].Value += 1
end
end
--end)
end)
local function UpdateWalkSpeed(plr, char)
local humanoid = char.Humanoid
local upgrades = plr:WaitForChild("upgrades"):GetChildren()
for i,upgrade in upgrades do
if upgrade.Name == "Speed" then
if upgrade.Value > 0 then
humanoid.WalkSpeed = 16*(1+(upgrade.Value*0.10))
elseif upgrade.Value<1 then
humanoid.WalkSpeed = 16
end
end
end
end
local function UpdateJumpPower(plr, char)
local humanoid = char.Humanoid
local upgrades = plr:WaitForChild("upgrades"):GetChildren()
for i,upgrade in upgrades do
if upgrade.Name == "Jump" then
if upgrade.Value > 0 then
humanoid.JumpPower = 50*(1+(upgrade.Value*0.10))
elseif upgrade.Value<1 then
humanoid.JumpPower = 50
end
end
end
end
local function playerAdded(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
charAdded(plr, char)
plr.CharacterAdded:Connect(function(char)
charAdded(plr, char)
end)
end
function charAdded(plr, char)
local humanoid = char.Humanoid
humanoid.Died:Connect(function()
plr:WaitForChild("leaderstats").Deaths.Value +=1
end)
UpdateWalkSpeed(plr, char)
UpdateJumpPower(plr, char)
end
for _, player in ipairs(game.Players:GetPlayers()) do
coroutine.wrap(playerAdded)(player)
end
game.Players.PlayerAdded:Connect(function(plr)
getData(plr)
playerAdded(plr)
game.ServerStorage.GemIncrement.Value = 10*(1+(plr.upgrades.Gems.Value*0.10))
for i,v in pairs(plr.upgrades:GetChildren()) do
v:GetPropertyChangedSignal("Value"):Connect(function() -- checks if any upgrade changes
upgradeChanged:FireClient(plr, v.Name)
warn("Upgraded", v.Name, "to", v.Value)
if v.Name=="Speed" then
UpdateWalkSpeed(plr, plr.Character)
elseif v.Name=="Jump" then
UpdateJumpPower(plr, plr.Character)
elseif v.Name=="Gems" then
game.ServerStorage.GemIncrement.Value = 10*(1+(v.Value*0.10))
end
end)
end
end)
game.Players.PlayerRemoving:Connect(saveData)