How can i remove that delayed animation on my viewmodel?

I’m making a game that consists backrooms, but for some reason, my flashlight viewmodel it’s doing a little delay when i press F, here’s a video of what i’m talking about:


Can you see that it shows my default arm without animation for a little bit? How can i fix that?

Here’s my code:


local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local runservice = game:GetService("RunService")

local mouse = game.Players.LocalPlayer:GetMouse()

local viewmodelanimation = nil

function Light()
	local player = game.Players.LocalPlayer
	local playerChar = player.Character
	local playerLight = playerChar.Head:FindFirstChild("Light")

	if playerLight then
		playerLight:Destroy()
		if camera:FindFirstChild("ViewModel") ~= nil then
			camera.ViewModel:Destroy()
		end
	else
		local viewmodel = game.ReplicatedStorage.ViewModel:Clone()
		local light = Instance.new("SurfaceLight",playerChar:FindFirstChild("Head"))
		
		light.Name = "Light"
		light.Range = 60 -- Change lighting range / Изменение диапозона освещения
		light.Angle = 50 -- Change lighting angle / Изменение угла освещения
		light.Shadows = true
		light.Brightness = 0.7 -- Change lighting brightness / Изменение яркости освещения
		viewmodel.Parent = camera
		
		character.Humanoid.HealthChanged:Connect(function(health)
			if health <= 0 then
				if camera:FindFirstChild("ViewModel") ~= nil then
					camera.ViewModel:Destroy()
				end
			end
		end)
		
		if camera:FindFirstChild("ViewModel") ~= nil then
			--local viewmodelanimation:AnimationTrack = camera.ViewModel.Humanoid:LoadAnimation(camera.ViewModel.Equip)
			local viewmodelcam = camera:FindFirstChild("ViewModel")
			viewmodelanimation = viewmodelcam.Humanoid:LoadAnimation(viewmodelcam.Equip)
			viewmodelanimation:Play(-1, 1, 1)
			viewmodelanimation:GetMarkerReachedSignal("End"):Connect(function()
				viewmodelanimation = camera.ViewModel.Humanoid:LoadAnimation(camera.ViewModel.Idle)
				viewmodelanimation:Play(0, 1, 1)
			end)
		end
		
		runservice.RenderStepped:Connect(function()
			
			if camera:FindFirstChild("ViewModel") ~= nil then
				camera.ViewModel:SetPrimaryPartCFrame(camera.CFrame * CFrame.new(.025, -1.5, -1))
			end
		end)
		
		viewmodel.BodyColors.LeftArmColor = character:WaitForChild("Body Colors").LeftArmColor
		viewmodel.BodyColors.RightArmColor = character:WaitForChild("Body Colors").RightArmColor
		
	end
end

mouse.KeyDown:connect(function(key)

	key = key:lower()
	if key == "f" then
		script.FlashlightSound:Play()
		Light()
	end
end)
1 Like

Maybe it’s due to the fact you insert the viewmodel first and don’t run the animation directly after, or maybe the animation you made doesn’t start from the beginning.

1 Like

i putted on the first keyframe ever, probably’s the second option, could an example code be given?

Give me a moment I’ll see if I can.

1 Like

okay, i’m gonna wait (Also i think i found a temporary solution)

I couldn’t really check whether you’re animation works or not, but hopefully the way I wrote it would fix that, unless it doesn’t I’ll possibly need the id to do anything else.

Hopefully I didn’t miss name anything so it’ll function as the original without issues.

Script
local Players, ReplicatedStorage, RunService, Debris, UserInputService = game:GetService("Players"), game:GetService("ReplicatedStorage"), game:GetService("RunService"), game:GetService("Debris"), game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Mouse = Players.LocalPlayer:GetMouse()
local Camera = workspace.CurrentCamera

local ViewModel, ViewModelAnimation, Connection = nil, nil, nil

local function SpawnViewport(Remove)
	ViewModel = Camera:FindFirstChild("ViewModel")

	if ViewModel == nil then
		ViewModel = ReplicatedStorage.ViewModel:Clone()

		Character["Body Colors"]:Clone().Parent = ViewModel
		ViewModel.Parent = Camera

		Connection = RunService.RenderStepped:Connect(function()
			if ViewModel ~= nil then
				ViewModel:SetPrimaryPartCFrame(Camera.CFrame * CFrame.new(.025, -1.5, -1))
			else
				Connection:Disconnect()
				Connection = nil
			end
		end)
	elseif ViewModel ~= nil and Remove then
		if Connection ~= nil then
			Connection:Disconnect()
			Connection = nil
		end
		SpawnViewport()
		Debris:AddItem(ViewModel, 0)
	end
end

local LightEnabled = false

function Light(Useless, Enabled)
	local Flashlight = Character.Head:FindFirstChild("Light")

	script.FlashlightSound:Play()

	if Flashlight then
		Debris:AddItem(Flashlight, 0)
	else
		Flashlight = Instance.new("SurfaceLight", Character:FindFirstChild("Head"))

		Flashlight.Name = "Light"
		Flashlight.Range = 60 -- Change lighting range / Изменение диапозона освещения
		Flashlight.Angle = 50 -- Change lighting angle / Изменение угла освещения
		Flashlight.Shadows = true
		Flashlight.Brightness = 0.7 -- Change lighting brightness / Изменение яркости освещения
	end

	if Enabled then
		SpawnViewport()
	else
		SpawnViewport(true)
	end

	--local viewmodelanimation:AnimationTrack = camera.ViewModel.Humanoid:LoadAnimation(camera.ViewModel.Equip)

	if ViewModel ~= nil then
		ViewModelAnimation = ViewModel.Humanoid:LoadAnimation(ViewModel.Equip)
		ViewModelAnimation:Play(-1, 1, 1)

		ViewModelAnimation:GetMarkerReachedSignal("End"):Connect(function()
			ViewModelAnimation = ViewModel.Humanoid:LoadAnimation(ViewModel.Idle)
			ViewModelAnimation:Play(0, 1, 1)
		end)
	end
end

Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function(Health)
	if Health <= 0 then
		SpawnViewport(true)
	end
end)

UserInputService.InputBegan:Connect(function(Input, Processed)
	if not Processed then
		if Input.KeyCode == Enum.KeyCode.F then
			LightEnabled = not LightEnabled
			Light(LightEnabled)
		end
	end
end)
1 Like

I had the same problem, you have to set the viewmodels position somewhere where it cant be seen(for example under the map) when you first clone it

1 Like

Or you could just have the animation running within the model before it parents itself.

1 Like

I tried setting the initial transparency arms and flashlight handle to 1 and then it wait’s a few seconds (0.03 seconds to be more exact) and it just puts to 0 again, that fix my problem.

This may work, but i already have my solution, so, thanks anyway

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.