local replicatedStorage = game:GetService(“ReplicatedStorage”)
local DatastoreService = game:GetService(“DataStoreService”)
local Players = game:GetService(“Players”)
local datastore = DatastoreService:GetDataStore(“MyDataStore”)
local characterShop = replicatedStorage.CharacterShop
local characters = characterShop.Characters
local remotes = characterShop.Remote
game.Players.PlayerAdded:Connect(function(player)
local characterShop = Instance.new(“Folder”)
characterShop.Name = “CharacterShop”
characterShop.Parent = player
local ownedCharacters = Instance.new("Folder")
ownedCharacters.Name = "Characters"
ownedCharacters.Parent = characterShop
local equippedCharacter = Instance.new("StringValue")
equippedCharacter.Name = "EquippedCharacter"
equippedCharacter.Value = "nil"
equippedCharacter.Parent = characterShop
local _characters = nil
local success, err = pcall(function()
_characters = datastore:GetAsync(player.UserId.."Characters")
end)
if _characters == nil then return end
if success and _characters ~= nil then
for i, v in pairs(_characters) do
local newVal = Instance.new("StringValue")
newVal.Name = v
newVal.Parent = ownedCharacters
end
else
warn(err)
end
end)
local function playerRemoving(player)
local _characters = {}
local success, err = pcall(function()
for i, v in pairs(player.CharacterShop.Characters:GetChildren()) do
table.insert(_characters, v.Name)
end
datastore:SetAsync(player.UserId.."Characters", _characters)
end)
wait(6)
if success then
print("Success")
else
warn(err)
end
end
remotes.Buy.OnServerInvoke = function(player, tempName)
local character = characters:FindFirstChild(tempName)
if character == nil then return end
local price = character.Price.Value
local currentChar = player.Character
local pos = currentChar.PrimaryPart.CFrame
if player.CharacterShop.Characters:FindFirstChild(character.Name) and player.CharacterShop.EquippedCharacter.Value == character.Name then
return "Character Already Equipped"
elseif player.CharacterShop.Characters:FindFirstChild(character.Name) and player.CharacterShop.EquippedCharacter.Value ~= character.Name then
local newCharacter = character:Clone()
newCharacter.Name = player.Name
player.Character = newCharacter
newCharacter.Parent = workspace
newCharacter.PrimaryPart.CFrame = pos
return "Equip"
elseif not player.CharacterShop.Characters:FindFirstChild(character.Name) and player.CharacterShop.EquippedCharacter.Value ~= character.Name then
if tonumber(player.leaderstats[character.Currency.Value].Value) >= tonumber(price) then
player.leaderstats[character.Currency.Value].Value -= price
local charcterVal = Instance.new("StringValue")
charcterVal.Name = character.Name
charcterVal.Parent = player.CharacterShop.Characters
local newCharacter = character:Clone()
newCharacter.Name = player.Name
player.Character = newCharacter
newCharacter.Parent = workspace
newCharacter.PrimaryPart.CFrame = pos
return "Buy"
else
return "Failed"
end
end
end
Players.PlayerRemoving:Connect(playerRemoving)
game:BindToClose(function()
wait(6)
for i, v in pairs(Players:GetPlayers()) do
playerRemoving(v)
end
end)