Ive been having a issue with weapon swapping in my game the issue is
that the animations arent loading, weapon swapping works by teleporting the weapon viewmodel to the camera and when you switch weapons you teleports another and teleports the unused viewmodels to cframe.new, when the game starts the animations all get loaded on the animation controller but animation only works on one view model
local Weapon = 2
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game["Players"]["LocalPlayer"]
local ScreenAmmo = Player.PlayerGui.Ammo
local Mouse = Player:GetMouse()
local MouseDown = 0
local Shooting = 0
local MaxAmmo = nil
local FOV = Instance.new("NumberValue")
FOV.Parent = workspace
FOV.Name = "FOV Slider Value"
FOV.Value = 0
local WeaponFolder = ReplicatedStorage.Viewmodels:Clone()
WeaponFolder.Parent=workspace.CurrentCamera
if Weapon == 1 then
MaxAmmo = 10
else
MaxAmmo = 20
end
local Ammo = MaxAmmo
local UIS = game:GetService("UserInputService")
local Viewmodel: Model = WeaponFolder["v"..Weapon]
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
local AnimationPlayer = Viewmodel:FindFirstChildOfClass("AnimationController")
local DamageEvent = ReplicatedStorage["DamageEvent"]
local Animations = {
reload1_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.reload1),
idle1_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.idle1),
fire1_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.fire1),
reload2_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.reload2),
idle2_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.idle2),
fire2_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.fire2)
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Mouse.Button1Down:Connect(function()
MouseDown=1
end)
Mouse.Button1Up:Connect(function()
MouseDown=0
end)
game:GetService("RunService"):BindToRenderStep("Viewmodel", 301, function(DT)
if Weapon == 1 then
MaxAmmo = 10
else
MaxAmmo = 20
end
for i=0,2 do
if i ~= Weapon then
local View:Model = WeaponFolder["v"..i]
View:PivotTo(CFrame.new())
end
end
if Ammo > MaxAmmo then
Ammo = MaxAmmo
end
if Weapon > 2 then
Weapon = 2
end
if Weapon < 0 then
Weapon = 0
end
Viewmodel = WeaponFolder["v"..Weapon]
AnimationPlayer = Viewmodel:FindFirstChildOfClass("AnimationController")
Viewmodel:PivotTo(workspace.CurrentCamera.CFrame)
Viewmodel:PivotTo(Viewmodel.PrimaryPart.CFrame + Viewmodel.PrimaryPart.CFrame.LookVector * FOV.Value)
ScreenAmmo.TextLabel2.Text = "Weapon:"..Weapon
Animations["idle"..Weapon.."_animation"]:Play()
if Shooting == 0 and Ammo > 0 then
Shooting = 1
if MouseDown == 1 then
Animations["fire"..Weapon.."_animation"]:Play()
local Sound = script.Fire:Clone() Sound.Parent = workspace
if Mouse.Target then
if Mouse.Target.Parent:IsA("Model") then
local Object = Mouse.Target.Parent
local Humanoid: Humanoid = Object:FindFirstChild("Humanoid")
if Humanoid then
DamageEvent:FireServer(Humanoid,10)
end
end
if Mouse.Target.Parent.Name == "Breakable" then
ReplicatedStorage["UnanchorPart"]:FireServer(Mouse.Target)
end
end
Sound:Play()
Ammo-=1
if Ammo < 0 then
Ammo = 0
end
if Weapon == 1 then
while MouseDown == 1 do
wait(0.1)
end
else
wait(0.1)
end
end
Shooting = 0
end
UIS.InputBegan:Connect(function(key,event)
if key.KeyCode == Enum.KeyCode.R then
if Ammo == 0 then
Shooting = 1
Animations["reload"..Weapon.."_animation"]:Play()
local Sound = script.Reload:Clone() Sound.Parent = workspace
Sound:Play()
for i=1,MaxAmmo do
Ammo += 1
wait(0.2)
end
Shooting = 0
end
end
end)
ScreenAmmo.TextLabel.Text = Ammo.."/"..MaxAmmo
end)
local hum: Humanoid = Player.Character["Humanoid"]
hum.Died:Connect(function()
Viewmodel:Destroy()
end)
ReplicatedStorage["SentAmmo"].OnClientEvent:Connect(function(num)
Ammo += num
end)
UIS.InputBegan:Connect(function(key)
if Shooting == 0 then
if key.KeyCode == Enum.KeyCode.Q then
Weapon -= 1
wait(1)
end
if key.KeyCode == Enum.KeyCode.E then
Weapon += 1
wait(1)
end
end
end)
if thats not enough you can view the whole project here (uncopylocked)