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

I am asking this again cuz I didnt get an answer, Character is not a valid member what should I do? [closed]

Asked by 3 years ago
Edited 3 years ago

This question already has an answer here:

Character is not a valid member of Model what should I do?
local function equipPet(player,pet)

    local character =  player.Character--problem is here

    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.CFrame

        local attachmentCharacter = Instance.new("Attachment")
        attachmentCharacter.Visible = false
        attachmentCharacter.Name = "attachmentCharacter"
        attachmentCharacter.Parent = character.HumanoiRootPart
        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.PrimaryPart

        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



game.Players.PlayerAdded:Connect(function(player)
    local inventory = Instance.new("Folder")
    inventory.Name = "PetInventory"
    inventory.Parent = player

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

    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)
    wait(5)
    equipPet(game.ReplicatedStorage.Pets.Storm)

end)

Problem is at line 3

0
Please code block ALL of your script. Secondly, tell us what the character is not a valid member of? Dovydas1118 1495 — 3y
0
I have no idea what you mean. Please explain clearly! Lightning_Game27 232 — 3y
0
Click edit, click the code block, Copy and paste ALL of your code in it. Also, provide more of an explanation, such as what are you trying to accomplish? What is the character not a valid member of? Dovydas1118 1495 — 3y
0
Just do local character = player.Character or player.CharacterAdded:Wait() DarkDanny04 407 — 3y
View all comments (2 more)
0
Problem is at line 3,I am trying to add pets into my game but I get error -Character is not a valid member of a model zuperlukas -7 — 3y
0
And local character = player.Character or player.CharacterAdded:Wait() didnt work zuperlukas -7 — 3y

Marked as Duplicate by User#29913, Dovydas1118, IAmNotTheReal_MePipe, synkrio, mixgingengerina10, and Fifkee

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Try This

local function equipPet(player,pet)
    repeat wait() until game:GetService('Players'):FindFirstChild(player).Character
    local character =  game:GetService('Players'):FindFirstChild(player).Character

    if pet ~= nil and character ~= nil then

        if character:FindFirstChild(player.."'s pet") then character[player.."'s pet"]:Destroy() end

        if character.HumanoidRootPart:FindFirstChild("attachmentCharacter")then 
            character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy() 
        end

        pet.Name = player.."'s pet"        

        pet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)

        local modelSize = pet.PrimaryPart.CFrame

        local attachmentCharacter = Instance.new("Attachment")
        attachmentCharacter.Visible = false
        attachmentCharacter.Name = "attachmentCharacter"
        attachmentCharacter.Parent = character:WaitForChild('HumanoiRootPart')
        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.PrimaryPart

        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



game.Players.PlayerAdded:Connect(function(player)
    local inventory = Instance.new("Folder")
    inventory.Name = "PetInventory"
    inventory.Parent = player

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

    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.Name,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
            end
        end
    end)
    wait(5)
    equipPet(game.ReplicatedStorage.Pets.Storm)

end)
0
Now I get this ServerScriptService.MainScript:2: attempt to index nil with 'Character' zuperlukas -7 — 3y
Ad