I want to replace the message outs that i used to play animations at the right moment because they do not work.
Here is the server and client scripts
Server:
local UpdateAmmoEvent = game.ReplicatedStorage:WaitForChild("UpdateAmmoCountGlock17")
local ammoLeft = 17
local reloading = false
local function reload(manual)
if reloading then return end
if ammoLeft == 17 then return end
reloading = true
ReloadSound:Play()
if manual and ammoLeft == 0 then
print("ReloadGlock17Empty")
else
print("ReloadGlock17NotEmpty")
end
wait(2.3)
ammoLeft = 17
UpdateAmmoEvent:FireAllClients(ammoLeft)
reloading = false
end
function PlayFireSound()
local NewSound = FireSound:Clone()
local MuzzleFlash = script.Parent.Handle.MuzzleFlash.MuzzleEffect
NewSound.Parent = Handle
NewSound:Play()
MuzzleFlash:Emit(20)
if ammoLeft == 0 then
print("GunFiredGlock17Last")
else
print("GunFiredGlock17")
end
Debris:AddItem(NewSound, NewSound.TimeLength)
end
function Fire(direction)
if Tool.Parent:IsA("Backpack") then return end
local directionalCF = CFrame.new(Vector3.new(), direction)
if ammoLeft > 0 and not reloading then
local direction = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local humanoidRootPart = Tool.Parent:WaitForChild("HumanoidRootPart", 1)
local myMovementSpeed = humanoidRootPart.Velocity
local modifiedBulletSpeed = (direction * BULLET_SPEED)
if PIERCE_DEMO then
CastBehavior.CanPierceFunction = CanRayPierce
end
local simBullet = Caster:Fire(FirePointObject.WorldPosition, direction, modifiedBulletSpeed, CastBehavior)
ammoLeft -= 1
UpdateAmmoEvent:FireAllClients(ammoLeft)
PlayFireSound()
else
reload(false) -- Automatically triggered reload
end
end
function ManualReload()
if ammoLeft == 1 then
reload(false) -- Trigger automatic reload animation
else
reload(true) -- Manually triggered reload
end
end
Client:
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local LogService = game:GetService("LogService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local function setupViewModel()
-- Destroy existing viewmodel if it exists
if workspace.CurrentCamera:FindFirstChild("ViewModelGlock17") then
workspace.CurrentCamera.ViewModelGlock17:Destroy()
end
-- Clone the viewmodel from ReplicatedStorage
local viewmodel = ReplicatedStorage:WaitForChild("ViewModelGlock17"):Clone()
viewmodel.Parent = workspace.CurrentCamera
-- Get the Animator
local animator = viewmodel:WaitForChild("Humanoid"):WaitForChild("Animator")
-- Load the animations
local Ranimation = Instance.new("Animation")
Ranimation.AnimationId = "rbxassetid://72523018738433" -- Replace with your animation ID
local RanimationTrack = animator:LoadAnimation(Ranimation)
local REanimation = Instance.new("Animation")
REanimation.AnimationId = "rbxassetid://97988611711973" -- Replace with your animation ID
local REanimationTrack = animator:LoadAnimation(REanimation)
local REEmptyAnimation = Instance.new("Animation")
REEmptyAnimation.AnimationId = "rbxassetid://120156932108231" -- Replace with your animation ID for empty reload
local REEmptyAnimationTrack = animator:LoadAnimation(REEmptyAnimation)
local RanimationL = Instance.new("Animation")
RanimationL.AnimationId = "rbxassetid://86438961102865" -- Replace with your animation ID for last shot
local RanimationLTrack = animator:LoadAnimation(RanimationL)
local WalkAnimation = Instance.new("Animation")
WalkAnimation.AnimationId = "rbxassetid://111477854942676" -- Replace with your animation ID
local WalkAnimationTrack = animator:LoadAnimation(WalkAnimation)
WalkAnimationTrack.Looped = true
-- Function to stop all playing animations
local function stopAllAnimations()
for _, track in ipairs(animator:GetPlayingAnimationTracks()) do
track:Stop()
end
end
local camera = workspace.CurrentCamera
local function moveCameraUp()
local currentCFrame = camera.CFrame
local newCFrame = currentCFrame * CFrame.Angles(math.rad(1), 0, 0)
camera.CFrame = newCFrame
end
-- Function to play the animations
local function RecoilAnimation()
stopAllAnimations()
RanimationTrack:Play()
end
local function ReloadAnimation()
stopAllAnimations()
REanimationTrack.Priority = Enum.AnimationPriority.Action4
REanimationTrack:Play()
end
local function ReloadEmptyAnimation()
stopAllAnimations()
REEmptyAnimationTrack:Play()
end
local function LastShotAnimation()
stopAllAnimations()
RanimationLTrack:Play()
end
-- Lock position at the end of the last shot animation
RanimationLTrack.Stopped:Connect(function()
local lastFrameCFrame = viewmodel.PrimaryPart.CFrame -- Assuming PrimaryPart is set
viewmodel:SetPrimaryPartCFrame(lastFrameCFrame)
end)
-- Connect the log messages to the animations
LogService.MessageOut:Connect(function(message, messageType)
if message == "GunFiredGlock17" then
moveCameraUp()
RecoilAnimation()
end
if message == "GunFiredGlock17Last" then
moveCameraUp()
LastShotAnimation()
end
if message == "ReloadGlock17NotEmpty" then
ReloadAnimation()
elseif message == "ReloadGlock17Empty" then
ReloadEmptyAnimation()
end
end)
-- Function to handle walking animation
local function updateWalkingAnimation()
if character.Humanoid.MoveDirection.Magnitude > 0 then
if not WalkAnimationTrack.IsPlaying then
WalkAnimationTrack:Play()
end
else
if WalkAnimationTrack.IsPlaying then
WalkAnimationTrack:Stop()
end
end
end
-- Connect the walking animation update to the RenderStepped event
RunService.RenderStepped:Connect(updateWalkingAnimation)
end
-- Function to handle tool equipped
local function onToolEquipped(tool)
if tool.Name == "Glock 17" then
setupViewModel()
end
end
-- Connect the tool equipped event
player.Character.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
onToolEquipped(child)
end
end)
-- Handle tool replacement
player.Character.ChildRemoved:Connect(function(child)
if child:IsA("Tool") and child.Name == "Glock 17" then
setupViewModel()
end
end)