This script isn't working?

This script is not working… don’t know why? I tried putting prints everywhere and they all printed. No errors.

Serverscript inside a part I’m using to script pets.

local pet = script.Parent

function givePet (player)
    if player then
        repeat wait() until player.Character
        local character = player.Character
        if character then
            local focusPart = character.UpperTorso
            local newPet = pet:Clone ()
            newPet.Parent = character
            local bodyPos = Instance.new("BodyPosition", newPet)
            bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
            
            local bodyGyro = Instance.new("BodyGyro", newPet)
            bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
            while wait() do
                    bodyPos.Position = focusPart.Position + Vector3.new(2, 2, 3)
                    bodyGyro.CFrame = focusPart.CFrame
            end
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        givePet(player)
    end)
end)
1 Like

What is the script supposed to do? So people can figure out the problem much more easier.

^
[spoiler](30 characters smhsmh)[/spoiler]

Huh what

30 characters smh

Well tell us what is wrong. Does the pet not move? Does it not appear at all? We need more information.

Pet is stationary until a player resets. Then the pet visits the dead body. The pet doesn’t go to a player after they respawn.

Check if maybe the pet is anchored?

Maybe do the same when player dies? Use
Humanoid:Died:Connect(function() and add wait to givepet fuction.

I want to point out some few things here:

  1. The repeat wait() until player.Character is not needed when you can just pass the character from the CharacterAdded.
  2. This also goes to the if player then and if character then as the PlayerAdded and CharacterAdded is a sign that the player and character now exists.
  3. I suggest you to not use the 2nd argument of Instance.new.

Because of this reason, your code can be like this:

function givePet (char)
    local focusPart = char.HumanoidRootPart
            local newPet = pet:Clone ()
            newPet.Parent = char

            local bodyPos = Instance.new("BodyPosition")
            bodyPos.Parent = newPet
            bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

            local bodyGyro = Instance.new("BodyGyro")
            bodyGyro.Parent = newPet
            bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
            while wait() do
                    bodyPos.Position = focusPart.Position + Vector3.new(2, 2, 3)
                    bodyGyro.CFrame = focusPart.CFrame
            end
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        givePet(char)
    end)
end)

Hey. Thanks for your input but that’s not what I am looking for. I added several little checks to make sure the character existed because the script was not functioning. When the pet wasn’t going to the player I added the repeat wait() until player.Character.

Adding on to what you said I can use

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(char)
end)

Instead of the longer version you had. The reason I had multiple checks to see if the player and character existed was to make sure that the player and the character existed. Like I said, the script wasn’t functioning so trial, error and testing is the only way to effectively debug.

I don’t think that would make a difference. CharacterAdded listens for when the character is spawned and when the player dies the character spawns.

Aren’t you supposed to have some thing in the wait () for the time?

wait() is equal to the smallest amount of time you can wait.

Position overrides anchoring.

True, but I do not see Position used. Only BodyPosition which is physics based and does not work anchored.