Script doesnt stop the animation

  1. I want to stop the animation when a value changed

  2. Script doesnt stop the animation

  3. I tried to get help from documentation and other websites but couldnt find any fix

So after the value changes to “Pickaxe” it plays the animation but after the value changes to " " it doesnt stop the animation but everything else works.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RE = ReplicatedStorage.RE.ViewModeltem
local PlayerAnims = ReplicatedStorage.PlayerAnims

local animations = {
	["Pickaxe"] = PlayerAnims.Pickaxe
}

function ChangeViewModelItemName(player, slot, guiItem, ItemClass)
	local ViewModelCurrentItem = player.PlayerGui.ViewModelCurrentItem
	local ViewModelSlotNumber = player.PlayerGui.ViewModelSlotNumber
	local ViewModelItemClass = player.PlayerGui.ViewModelItemClass
	ViewModelCurrentItem.Value = guiItem
	ViewModelSlotNumber.Value = slot
	if ItemClass ~= nil then
		ViewModelItemClass.Value = ItemClass
	else
		ViewModelItemClass.Value = ""
	end

	-- Equip on player Model
	local latestItem = player.PlayerGui.latestItemCharacterEquipped
	local Humanoid = player.Character:WaitForChild("Humanoid")

	-- Animations
	local PickaxeAnim = Humanoid:LoadAnimation(animations.Pickaxe)

	if ViewModelItemClass.Value == "" then
		
		player.Character.ItemPos.Item.Part1 = nil
		player.Character.ItemPos:FindFirstChild(latestItem.Value):Destroy()
		latestItem.Value = ""
		PickaxeAnim:Stop()
		player.Character.ItemPos.Item.C0 = CFrame.new(0,0,0)
		player.Character.ItemPos.Item.C1 = CFrame.Angles(0,0,0)
		
	elseif ViewModelItemClass.Value == "Pickaxe" then
		
		local ItemToCopy = ReplicatedStorage.CharItemModels:FindFirstChild(guiItem):Clone()
		ItemToCopy.Parent = player.Character.ItemPos
		player.Character.ItemPos.Item.Part1 = player.Character.ItemPos:FindFirstChild(guiItem)
		latestItem.Value = guiItem
		PickaxeAnim:Play()
		
	end
end

RE.OnServerEvent:Connect(ChangeViewModelItemName)
3 Likes

I scanned your code, and nothing seems wrong at first glance, however there is something you should consider changing. Loading the animation directly via:

Humanoid:LoadAnimation()

is deprecated

I would highly recommend using Humanoid.Animator:LoadAnimation(). Now this most likely isn’t the issue, but it’s worth a shot anyway, plus it’s always good to keep your scripts up-to-date. If this does fix the issue, please let me know! If not, please also let me know so I can try and find another solution.

Cheers,
Techy

5 Likes

The function keeps loading a new animation every time meaning it wont stop the old animation as its variable has been redeclared everytime because of where you placed it.
Try placing the local PickaxeAnim = Humanoid:LoadAnimation(animations.Pickaxe) outside of the function as then it should only use the same animation every time

You should also use Humanoid.Animator:LoadAnimation() instead

1 Like

Hey Techy,

I tried Humanoid.Animator:LoadAnimation() everything seems same as before but thank you for your advice. :slight_smile:

Hi Zolyphony,

How I am able to do that, beacause I send the player value to the function ? I am sorry if I am asking a easy question.

when you have no tool it does:
PickaxeAnim:Stop()

when there is a tool it does:
Pickaxe:Play()

PickaxeAnim and Pickaxe are two different things, you need them to be the same for it to stop the animation (I think)

So you should try making both of them PickaxeAnim

oops sorry I made a typo on there its the same as you said in the original script.

try putting this line: local PickaxeAnim = Humanoid:LoadAnimation(animations.Pickaxe)
out of the function, because i think it creates a new one every time the function is called

someone already said this

where is this script located in?

1 Like

Script is located in ServerScriptService.

try this



local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RE = ReplicatedStorage.RE.ViewModeltem
local PlayerAnims = ReplicatedStorage.PlayerAnims

local animations = {
	["Pickaxe"] = PlayerAnims.Pickaxe
}

local plarname = nil

game:GetService("Players").PlayerAdded:Connect(function(plr)
	wait()
	plarname = plr.Name
end)
repeat wait() until plarname ~= nil
print(plarname)
local plar = game.Players:FindFirstChild(plarname)
local PickaxeAnim = plar.Humanoid:LoadAnimation(animations.Pickaxe)

function ChangeViewModelItemName(player, slot, guiItem, ItemClass)
	local ViewModelCurrentItem = player.PlayerGui.ViewModelCurrentItem
	local ViewModelSlotNumber = player.PlayerGui.ViewModelSlotNumber
	local ViewModelItemClass = player.PlayerGui.ViewModelItemClass
	ViewModelCurrentItem.Value = guiItem
	ViewModelSlotNumber.Value = slot
	if ItemClass ~= nil then
		ViewModelItemClass.Value = ItemClass
	else
		ViewModelItemClass.Value = ""
	end

	-- Equip on player Model
	local latestItem = player.PlayerGui.latestItemCharacterEquipped
	local Humanoid = player.Character:WaitForChild("Humanoid")

	-- Animations

	if ViewModelItemClass.Value == "" then

		player.Character.ItemPos.Item.Part1 = nil
		player.Character.ItemPos:FindFirstChild(latestItem.Value):Destroy()
		latestItem.Value = ""
		PickaxeAnim:Stop()
		player.Character.ItemPos.Item.C0 = CFrame.new(0,0,0)
		player.Character.ItemPos.Item.C1 = CFrame.Angles(0,0,0)

	elseif ViewModelItemClass.Value == "Pickaxe" then

		local ItemToCopy = ReplicatedStorage.CharItemModels:FindFirstChild(guiItem):Clone()
		ItemToCopy.Parent = player.Character.ItemPos
		player.Character.ItemPos.Item.Part1 = player.Character.ItemPos:FindFirstChild(guiItem)
		latestItem.Value = guiItem
		PickaxeAnim:Play()

	end
end

RE.OnServerEvent:Connect(ChangeViewModelItemName)
1 Like

Hey, I get a error like this :
Humanoid is not a valid member of Player “Players.efsane14010” - Studio
Stack Begin - Studio
Script ‘ServerScriptService.ViewModelItem’, Line 24

also can you explain what the code does i really didnt understand :smile:

it should work now

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RE = ReplicatedStorage.RE.ViewModeltem
local PlayerAnims = ReplicatedStorage.PlayerAnims

local animations = {
	["Pickaxe"] = PlayerAnims.Pickaxe
}

local char = nil --the character of the player has not been defined yet(the script does not know who the player is yet)

game:GetService("Players").PlayerAdded:Connect(function(plr)--when a player joins the game it activates the function and gets the player(that is in game.Players)
	plr.CharacterAdded:Connect(function(Char) --when the character of the player that joined loaded in, it activates this function and gets the character of that player(that is in workspace)
		wait()
		char = Char -- the local char that first was nothing(nil) is now the character that was added 
	end)
end)


repeat wait() until char ~= nil -- pauzes the script until char is not nothing(nil) anymore, but so it is a character (the character has been defined)
print(char.Parent)
local PickaxeAnim = char.Humanoid:LoadAnimation(animations.Pickaxe)

function ChangeViewModelItemName(player, slot, guiItem, ItemClass)
	local ViewModelCurrentItem = player.PlayerGui.ViewModelCurrentItem
	local ViewModelSlotNumber = player.PlayerGui.ViewModelSlotNumber
	local ViewModelItemClass = player.PlayerGui.ViewModelItemClass
	ViewModelCurrentItem.Value = guiItem
	ViewModelSlotNumber.Value = slot
	if ItemClass ~= nil then
		ViewModelItemClass.Value = ItemClass
	else
		ViewModelItemClass.Value = ""
	end

	-- Equip on player Model
	local latestItem = player.PlayerGui.latestItemCharacterEquipped
	local Humanoid = player.Character:WaitForChild("Humanoid")

	-- Animations

	if ViewModelItemClass.Value == "" then

		player.Character.ItemPos.Item.Part1 = nil
		player.Character.ItemPos:FindFirstChild(latestItem.Value):Destroy()
		latestItem.Value = ""
		PickaxeAnim:Stop()
		player.Character.ItemPos.Item.C0 = CFrame.new(0,0,0)
		player.Character.ItemPos.Item.C1 = CFrame.Angles(0,0,0)

	elseif ViewModelItemClass.Value == "Pickaxe" then

		local ItemToCopy = ReplicatedStorage.CharItemModels:FindFirstChild(guiItem):Clone()
		ItemToCopy.Parent = player.Character.ItemPos
		player.Character.ItemPos.Item.Part1 = player.Character.ItemPos:FindFirstChild(guiItem)
		latestItem.Value = guiItem
		PickaxeAnim:Play()

	end
end

RE.OnServerEvent:Connect(ChangeViewModelItemName)

Heyyyy I just found out where was the problem it happened because of

local plar = game.Players:FindFirstChild(plarname)
plar.Humanoid......
--it should be 
plar.Character.Humanoid....

Thank you so much Kyan but can you explain what the code actually do ? Its really confusing for me

plar.Character.Humanoid works as well, but 1 message before your reply(that I am replying to rn) I made an improved version with text what the code does

1 Like

Thank you soo much Kyan, now I understand everything and also learned what the problem was. Everything works Perfect. :happy4:

1 Like

Hey kyan soooo, I have a problem with the script again it does play the animation but somehow on wrong player. I couldnt figure how to fix the issue. (I will send video soon but rn im trying to fix another error that break all the scripts)

OK Here is the video:


I think you can see the problem here I dont know how to explain it.

the script probably has to be a localscript in startercharacterscripts for it to work, you would have to edit a few things though

because player1 joined first it only took player1 as the player

try to put this as a local script in startercharacterscripts (it might not work)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RE = ReplicatedStorage.RE.ViewModeltem
local PlayerAnims = ReplicatedStorage.PlayerAnims

local animations = {
	["Pickaxe"] = PlayerAnims.Pickaxe
}
wait()
local char = script.Parent
print(char)



local PickaxeAnim = char.Humanoid:LoadAnimation(animations.Pickaxe)

function ChangeViewModelItemName(player, slot, guiItem, ItemClass)
	if player.Name == char.Name then
	local ViewModelCurrentItem = player.PlayerGui.ViewModelCurrentItem
	local ViewModelSlotNumber = player.PlayerGui.ViewModelSlotNumber
	local ViewModelItemClass = player.PlayerGui.ViewModelItemClass
	ViewModelCurrentItem.Value = guiItem
	ViewModelSlotNumber.Value = slot
	if ItemClass ~= nil then
		ViewModelItemClass.Value = ItemClass
	else
		ViewModelItemClass.Value = ""
	end

	-- Equip on player Model
	local latestItem = player.PlayerGui.latestItemCharacterEquipped
	local Humanoid = player.Character:WaitForChild("Humanoid")

	-- Animations

	if ViewModelItemClass.Value == "" then

		player.Character.ItemPos.Item.Part1 = nil
		player.Character.ItemPos:FindFirstChild(latestItem.Value):Destroy()
		latestItem.Value = ""
		PickaxeAnim:Stop()
		player.Character.ItemPos.Item.C0 = CFrame.new(0,0,0)
		player.Character.ItemPos.Item.C1 = CFrame.Angles(0,0,0)

	elseif ViewModelItemClass.Value == "Pickaxe" then

		local ItemToCopy = ReplicatedStorage.CharItemModels:FindFirstChild(guiItem):Clone()
		ItemToCopy.Parent = player.Character.ItemPos
		player.Character.ItemPos.Item.Part1 = player.Character.ItemPos:FindFirstChild(guiItem)
		latestItem.Value = guiItem
		PickaxeAnim:Play()

	end
	end
end



RE.OnServerEvent:Connect(ChangeViewModelItemName)