Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to find child after reset?

Hello, so I have here a LocalScript in StarterCharacter that is a key bind animation. When you click a certain key on your keyboard, and an animation plays and continues to play until you click the key again.

When testing, I found that if a person resets or dies, the script can no longer find the child. The child is an animation named "Anim1" and that is the animation that plays. Here is the script I have currently.

local btn = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local CanEmote = true
local CoolDown = 0 
local debounce = false
local dance = hum:LoadAnimation(script:FindFirstChild("Anim1"))

dance.Looped = true

game:GetService("UserInputService").InputBegan:Connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.F then
        if debounce == false then
            debounce = true
            if CanEmote == true then
                hum.JumpPower = 0
                hum.WalkSpeed = 0
                CanEmote = true
                dance:Play()
            end
        elseif debounce == true then
            debounce = false
            dance:Stop()
            hum.WalkSpeed = 16
            hum.JumpPower = 50
        end
    end
end)

Does anyone know how to detect the child that is an animation? I want to understand how to do this, so If you could explain some things that would be great. Any help is appreciated, thank you.

1 comment

1

When a humanoid dies the character model deletes and all the scripts inside doesnt work. When they respawn the model replictes itsellf like and scripts dont work to fix this just add a while loop

1 answer

This is because somehow whenever the character dies, the script references those parts of the character to that old, dead character, and since the old character is nil those parts are also nil. A good and quick way to get a brand new character model.

I don't remember the full method but I think this is how it goes:

local Player = game.Players.LocalPlayer
local character = Player.Character

Player.CharacterAdded:Connect(function(NewCharacter)
	character = NewCharacter
end)

If you have body parts inside the character you can do the same thing.

local Player = game.Players.LocalPlayer
local character = Player.Character
local leftarm = character:FindFIrstChild("Left Arm") -- FindFirstChild wont error if not found

Player.CharacterAdded:Connect(function(NewCharacter)
	character = NewCharacter
	leftarm = Character:WaitForChild("Left Arm")
end)

FInally your script that is hopefully fixed:

local btn = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")

plr.CharacterAdded:Connect(function(NewCharacter)
	character = NewCharacter
end)

local CanEmote = true
local CoolDown = 0 
local debounce = false
local dance = hum:LoadAnimation(script:FindFirstChild("Anim1"))

dance.Looped = true

game:GetService("UserInputService").InputBegan:Connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.F then
        if debounce == false then
            debounce = true
            if CanEmote == true then
                hum.JumpPower = 0
                hum.WalkSpeed = 0
                CanEmote = true
                dance:Play()
            end
        elseif debounce == true then
            debounce = false
            dance:Stop()
            hum.WalkSpeed = 16
            hum.JumpPower = 50
        end
    end
end)

I found this on a devforum post long ago, I'll give you it when I find it.

Edit: here might be the fix for your animation problem, I just basically did the same thing:

local btn = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local dance

plr.CharacterAdded:Connect(function(NewCharacter)
	character = NewCharacter
	hum = character:WaitForChild("Humanoid")
	dance = hum:LoadAnimation(script:FindFirstChild("Anim1"))
end)

local CanEmote = true
local CoolDown = 0 
local debounce = false

dance.Looped = true

game:GetService("UserInputService").InputBegan:Connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.F then
        if debounce == false then
            debounce = true
            if CanEmote == true then
                hum.JumpPower = 0
                hum.WalkSpeed = 0
                CanEmote = true
                dance:Play()
            end
        elseif debounce == true then
            debounce = false
            dance:Stop()
            hum.WalkSpeed = 16
            hum.JumpPower = 50
        end
    end
end)

4 comments

0

I'm still getting the same errors in the OutPut, saying, "LoadAnimation requires an Animation Object" The line it talks about says, local dance = hum:LoadAnimation(script:FindFirstChild("Anim1"))

0

it's because of a different problem, you probably are referncing an object that isn't an animation or animation hasn't loaded yet. I'll edit my script since this scenario may work too.

0

Now I'm getting an error from line 17, dance.Looped = True, weird. It was able to go through the top part of the script but now it's interfering with the main part of the script. That's something I wanted to avoid.

0

What was the error the output directly said? Still not relevant to the former problem so that's a good thing

This discussion is preserved as it was. New answers, comments, and votes are closed.