Hello developers! I recently started transfering all datastores to DataStore 2. This is the character shop. Here it is:
local starterCharScripts = game.StarterPlayer.StarterCharacterScripts
local ChangeCamSubject = game.ReplicatedStorage.Remotes.ChangeCamSubject
local new = Instance.new
local datastore = game:GetService("DataStoreService"):GetDataStore("Characters")
local updateAsync = datastore.UpdateAsync
local loadData = function(player)
local id = player.UserId
local data
local success,output = pcall(function()
data = datastore:GetAsync(id)
end)
assert(success,output)
if not data then
data = {
Characters = {},
EquippedCharacter = "",
leaderstats = {}
}
end
if not data.leaderstats then data.leaderstats = {} end
for _,v in pairs(data.Characters) do
local value = new("StringValue")
value.Parent = player.Characters
value.Name = v
end
for _,v in pairs(player.leaderstats:GetChildren()) do
if data.leaderstats[v.Name] then
v.Value = data.leaderstats[v.Name]
end
end
player.EquippedCharacter.Value = data.EquippedCharacter
end
local saveData = function(player)
local id = player.UserId
local data = {
Characters = {},
EquippedCharacter = "",
leaderstats = {}
}
for _,v in pairs(player.Characters:GetChildren()) do
table.insert(data.Characters,v.Name)
end
for _,v in pairs(player.leaderstats:GetChildren()) do
data.leaderstats[v.Name] = v.Value
end
data.EquippedCharacter = player.EquippedCharacter.Value
local success,output = pcall(updateAsync,datastore,id,function(old)
return data
end)
assert(success,output)
end
local ChangeChar = function(player,charModel)
player:LoadCharacter()
local currentChar = player.Character
local pos = currentChar.PrimaryPart.CFrame
local animate = charModel:FindFirstChild('Animate')
local health = charModel:FindFirstChild('Health')
local curAnimate = currentChar:FindFirstChild("Animate")
local curHealth = currentChar:FindFirstChild("Health")
if not health and curHealth then
curHealth:Clone().Parent = charModel
curHealth:Destroy()
end
charModel.Name = player.Name
charModel.Parent = workspace
player.Character = charModel
charModel.PrimaryPart:SetNetworkOwner(player)
charModel.PrimaryPart.CFrame = pos
if not animate and curAnimate then
curAnimate:Clone().Parent = charModel
curAnimate:Destroy()
end
if not charModel.Humanoid:FindFirstChildOfClass("Animator") then
local animator = new('Animator')
animator.Parent = charModel.Humanoid
end
currentChar:Destroy()
for _,v in pairs(starterCharScripts:GetChildren()) do
if not charModel:FindFirstChild(v.Name) then
v:Clone().Parent = charModel
end
end
ChangeCamSubject:FireClient(player,charModel.Humanoid)
charModel.Humanoid.Died:Connect(function()
wait(game.Players.RespawnTime)
updateChar(player)
end)
end
updateChar = function(player)
local equippedChar = player.EquippedCharacter.Value
local chars = player.Characters
local curChar = player.Character or player.CharacterAdded:Wait()
local changing = player.ChangingChar
if changing.Value then
repeat wait(0.1) until not changing.Value
end
changing.Value = true
if chars:FindFirstChild(equippedChar) then
local module = game.ServerStorage.Characters:FindFirstChild(equippedChar)
if module then
local char = module:FindFirstChildOfClass("Model")
ChangeChar(player,char:Clone())
else
player:LoadCharacter()
end
else
player:LoadCharacter()
end
wait(0.1)
changing.Value = false
end
local onJoin = function(player)
-- // This is where we stored player money.
local stats = player:WaitForChild("leaderstats")
-- // This is where we store the purchased character.
local characterfolder = new("Folder")
characterfolder.Parent = player
characterfolder.Name = "Characters"
-- // Player's currency for the character shop.
local money = stats:WaitForChild("Points")
-- // Equipped Character Value
local EquippedCharacter = new("StringValue")
EquippedCharacter.Parent = player
EquippedCharacter.Name = "EquippedCharacter"
local changing = new("BoolValue")
changing.Parent = player
changing.Name = "ChangingChar"
loadData(player)
updateChar(player)
EquippedCharacter.Changed:Connect(function()
updateChar(player)
end)
end
game.Players.PlayerAdded:Connect(onJoin)
game.Players.PlayerRemoving:Connect(saveData)
If you can help, please let me know. Thanks, WE