Error on animation play

Module Script

function modAnimation:PlayAnimation(Player,Animation,AdjustSpeed,Mode)	
	local AnimationTrack = Instance.new("Animation")
	AnimationTrack.AnimationId = "https://www.roblox.com/item.aspx?id="..Animation
	AnimationTrack.Name = string.lower(Animation)
	local AnimationLoader = nil
	if game.Players:FindFirstChild(Player) then
	AnimationLoader = Player.Character.Humanoid:LoadAnimation(AnimationTrack)
	else
	AnimationLoader = Player.Humanoid:LoadAnimation(AnimationTrack)
	end
	AnimationLoader:Play()	
	if Mode == "Normal" or Mode == nil then
		AnimationLoader:AdjustSpeed(AdjustSpeed)		
		elseif Mode == "Divided" then
		AnimationLoader:AdjustSpeed(AnimationLoader.Length/AdjustSpeed)
		
	end
end

Combat Script

			modAnimation:PlayAnimation(plr.Character,modAnimation["Library"]["Inexperienced Combo 1"],(1.1+MoveTime))

The error happens in module script on this line it says
argument #1 expects a string, but Instance was passed
Anybody know how to fix this?

	if game.Players:FindFirstChild(Player) then

try this:

function modAnimation:PlayAnimation(Player, Animation, AdjustSpeed, Mode)    
    local AnimationTrack = Instance.new("Animation")
    AnimationTrack.AnimationId = "https://www.roblox.com/item.aspx?id="..Animation
    AnimationTrack.Name = string.lower(Animation)
    local AnimationLoader = nil
    
    local character
    if typeof(Player) == "string" then
        local player = game.Players:FindFirstChild(Player)
        if player then
            character = player.Character
        end
    else
        character = Player
    end
    
    if character and character:FindFirstChild("Humanoid") then
        AnimationLoader = character.Humanoid:LoadAnimation(AnimationTrack)
        AnimationLoader:Play()    
        
        if Mode == "Normal" or Mode == nil then
            AnimationLoader:AdjustSpeed(AdjustSpeed)        
        elseif Mode == "Divided" then
            AnimationLoader:AdjustSpeed(AnimationLoader.Length/AdjustSpeed)
        end
    end
end
1 Like

It simply means that you added an instance instead of a string inside of the parameter of FindFirstChild(). The parameter in FindFirstChild() needs to be a string so simply change

game.Players:FindFirstChild(Player)

to

game.Players:FindFirstChild(Player.Name)

give this a try

Player.Character is classed as a Model, which itself hails from the Instance class. Instance:FindFirstChild is documented as a function that searches for a child instance with a given name. The parameter for that given name is classed as a string:


Your function was scripted to use the “Player” parameter as the argument to a FindFirstChild call, and your calling code reveals that Player.Character was assigned to the “Player” parameter. This would effectively pass an instance to a function expecting a string, which would raise the error you received.

It seems you meant to verify the given character belonged to a player. To properly achieve this, use Players:GetPlayerFromCharacter. After phasing out redundancies and other issues with your function, you’re left with:

function modAnimation:PlayAnimation(character: Model & any, animationId: number, timeScale: number?, mode: number?)
	if not Players:GetPlayerFromCharacter(character) then
		return
	end
	
	local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
	if not humanoid then
		return
	end
	
	local animator = humanoid:FindFirstChildOfClass("Animator") :: Animator
	if not animator then
		return
	end
	
	-- Use the universal content prefix. There are no lower-case numbers.
	local animation       = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://" .. animationId
	animation.Name        = animationId
	
	-- The Humanoid can only be found as a child of a player's character.
	-- Humanoid:LoadAnimation was deprecated.
	local animationTrack = animator:LoadAnimation(animation)
	animationTrack:Play()
	
	-- Only adjust the speed when desired.
	if not timeScale then
		return
	end
	
	-- Modes need only adjust the time-scale.
	if mode == "Divded" then
		timeScale = animationTrack.Length / timeScale
	end
	
	-- No mode or a "Normal" mode uses existing time-scale.
	animationTrack:AdjustSpeed(timeScale)
end
2 Likes