Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

if i leave my game and then i rejoin all my pets that i have are gone. how can i fix that?

Asked by 2 years ago
Edited 2 years ago

this is my mainscript

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 = 25000
        alignPosition.Attachment0 = attachmentPet
        alignPosition.Attachment1 = attachmentCharacter
        alignPosition.Responsiveness = 25
        alignPosition.Parent = pet

        local alignOrientation = Instance.new("AlignOrientation")
        alignOrientation.MaxTorque = 25000
        alignOrientation.Attachment0 = attachmentPet
        alignOrientation.Attachment1 = attachmentCharacter
        alignOrientation.Responsiveness = 25
        alignOrientation.Parent = pet

        pet.Parent = character

    end

end

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("CoinsStats")

game.Players.PlayerAdded:Connect(function(Player)

    local leaderstats = Instance.new("Folder", Player)
    leaderstats.Name = "leaderstats"

    local Currency = Instance.new("IntValue", leaderstats)
    Currency.Name = "Coins"
    Currency.Value = 0

    local inventory = Instance.new("Folder")
    inventory.Name = "PetInventory"
    inventory.Parent = Player

    local equippedPet = Instance.new("StringValue")
    equippedPet.Name = "EquippedPet"
    equippedPet.Parent = Player


    local Data = DS:GetAsync(Player.UserId)
    if Data then
        Currency.Value = Data
    end

game.Players.PlayerRemoving:Connect(function(Player)
    DS:SetAsync(Player.UserId, Player.leaderstats.Coins.Value)
end)    

    Player.CharacterAdded:Connect(function(char)
        if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
            equipPet(Player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
        end
    end)

    equippedPet.Changed:Connect(function()
        if equippedPet.Value ~= nil then
            if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
                equipPet(Player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
            end
        end
    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)

Answer this question