Hi im trying to make a fully working leaderboard with leaderstats but… I get this error
This is the script ``
local DataStoreService = game:GetService(“DataStoreService”)
local PetDataStore = DataStoreService:GetDataStore(“PetDataStore”)
local function equipPet(player,pet)
local character = player.Character
if pet ~= nil and character ~= nil then
if character:FindFirstChild(player.Name.."'s Pet") then character[player.Name.."'s Pet"]:Destroy() end
if character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy() end
pet.Name = player.Name.."'s Pet"
pet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)
local modelSize = pet.PrimaryPart.Size
local attachmentCharacter = Instance.new("Attachment")
attachmentCharacter.Visible = false
attachmentCharacter.Name = "attachmentCharacter"
attachmentCharacter.Parent = character.HumanoidRootPart
attachmentCharacter.Position = Vector3.new(1,1,0) + modelSize
local attachmentPet = Instance.new("Attachment")
attachmentPet.Visible = false
attachmentPet.Parent = pet.PrimaryPart
local alignPosition = Instance.new("AlignPosition")
alignPosition.MaxForce = 30000
alignPosition.Attachment0 = attachmentPet
alignPosition.Attachment1 = attachmentCharacter
alignPosition.Responsiveness = 15
alignPosition.Parent = pet
local alignOriential = Instance.new("AlignOrientation")
alignOriential.MaxTorque = 25000
alignOriential.Attachment0 = attachmentPet
alignOriential.Attachment1 = attachmentCharacter
alignOriential.Responsiveness = 25
alignOriential.Parent = pet
pet.Parent = character
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local tokens = Instance.new("IntValue")
tokens.Name = "Tokens"
tokens.Value = 0
tokens.Parent = leaderstats
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Value = 0
kills.Parent = leaderstats
local Lv1 = Instance.new("IntValue")
Lv1.Name = "Levels"
Lv1.Value = 0
Lv1.Parent = leaderstats
local exp = Instance.new("IntValue")
exp.Name = "Exp"
exp.Value = 0
exp.Parent = leaderstats
local inventory = Instance.new("Folder")
inventory.Name = "PetInventory"
inventory.Parent = player
local eqippedPet = Instance.new("StringValue")
eqippedPet.Name = "EquippedPet"
eqippedPet.Parent = player
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function(killed)
local CreatorTag = character.Humanoid:FindFirstChild("creator")
local stats = CreatorTag.Value:WaitForChild("leaderstats")
stats["Tokens"].Value = stats["Tokens"].Value + 50
stats["Kills"].Value = stats["Kills"].Value + 1
stats["Exp"].Value = stats["Exp"].Value + 100
end)
end)
game.Players.PlayerAdded:Connect(function(plr)
wait(1)
local exp = plr.leaderstats.Exp
local levels = plr.leaderstats.Levels
while wait() do
if exp.Value >= (100 *(levels.Value + 1)) then
levels.Value = levels.Value + 1
exp.Value = 0
game.ReplicatedStorage.LevelUpGui:FireClient(plr)
end
end
end)
player.CharacterAdded:Connect(function(char)
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(eqippedPet.Value) then
equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(eqippedPet.Value):Clone())
end
end)
eqippedPet.Changed:Connect(function()
if eqippedPet.Value ~= nil then
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(eqippedPet.Value) then
equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(eqippedPet.Value):Clone())
end
end
end)
local data
local data = PetDataStore:GetAsync(player.UserId.."-pet")
if data then
-- e
for i, petName in pairs(data) do
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
local stringValue = Instance.new("StringValue")
stringValue.Name = petName
stringValue.Parent = player.PetInventory
end
end
game.ReplicatedStorage.SendData:FireClient(player,data)
print("Fried sent data")
else
print("No Pet Data found Must be a new player or corrupted data")
end
local equippedPetData = PetDataStore:GetAsync(player.UserId.."-equippedPet")
if equippedPetData then
equippedPetData.Value = equippedPetData
end
end)
local function saveData(player)
if player:FindFirstChild(“PetInventory”) then
local inventory = {}
for i, v in pairs(player.PetInventory:GetChildren()) do
table.insert(inventory,v.Name)
end
local success, errorMessage = pcall(function()
PetDataStore:SetAsync(player.UserId.."-pet",inventory)
end)
if success then
print("Data Saved")
else
print("Error: "..errorMessage)
end
end
if player:FindFirstChild("EquippedPet") then
if player.EquippedPet.Value ~= nil then
local success, errorMessage = pcall(function()
PetDataStore:SetAsync(player.UserId.."-equippedPet",player.Equipped.Value)
end)
end
end
end
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)
game.ReplicatedStorage.EquipPet.OnServerEvent:Connect(function(player,petName)
local pet = game.ReplicatedStorage.Pets:FindFirstChild(petName)
if pet and player.PetInventory:FindFirstChild(petName) then
player.EquippedPet.Value = petName
end
end)
game.ReplicatedStorage.UnequipPet.OnServerEvent:Connect(function(player)
player.EquippedPet.Value = “”
if player.Character:FindFirstChild(player.Name…"'s Pet") then
player.Character[player.Name…"'s Pet"]:Destroy()
end
if player.Character.HumanoidRootPart:FindFirstChild(“attachmentCharacter”) then
player.Character.HumanoidRootPart:FindFirstChild(“attachmentCharacter”):Destroy()
end
end)```
