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