I’m making a combat system framework. My problem is that on the server, when I tell the client to replicate the animation, I need to know when the animationEvent is reached so that I can sync the hit effects. Is there a way for the server to wait or run the code when the animationEvent is reached on the client?
EX:
Client tells server to m1
Server does processing, tells client to replicate m1
[how to communicate to server that animation event is reached here?]
when animation event is reached, server does processing and etc.
In a previous system, I tried to solve this problem by just dropping the code and continuing when a remote event was recieved on the server-side. However, I would really prefer not to use this method. I also don’t like using the client to handle the processing as well. If there is no way to do this, then lmk.
If I’m understanding correctly, you want the hit effect when the animation ends, so you can use this:
local anim = Instance.new("Animation")
local animator = Instance.new("Animator")
local track = animator:LoadAnimation(anim)
track.Ended:Connect(function()
-- code to run here
end)
If you want it when it reaches a certain point, you can use this:
local anim = Instance.new("Animation")
local animator = Instance.new("Animator")
local track = animator:LoadAnimation(anim)
track:GetMarkerReachedSignal("Name of KeyframeMarker"):Connect(function()
-- code to run here
end)
So how it works is that the server tells the CLIENT to play the animation. and the SERVER needs to know when it reaches a certain point. I want to be able to do that without remote events, but I think I already have another solution but thanks
Run the animation on the server. My eyes have been opened, why have we blindly been following the rule that animations must be done on client? Unless your game is extremely server-intensive, there is no downside to playing animations on server! If it’s ping, remote events and playing animations take the same amount of time to send. Either way, the player has network ownership over the character so the client will be handling their own animations either way. This so called “Blasphemy” of running animations on the server-side is nothing but an overexaggerated ruse!
If you don’t want to do that then:
Certainly! Here’s a basic example of the hybrid approach, where the server initiates the process and the client confirms when the animation event is reached, allowing the server to then handle the hit effect.
Setup
RemoteEvent: Create a RemoteEvent in ReplicatedStorage named CombatEvent.
Animation: Assume you have an attack animation with an event called "Hit" to signify when the hit effect should trigger.
Script Breakdown
In this approach:
The server listens for the player’s attack command.
It validates the action, processes any necessary server-side logic, and instructs the client to start the animation.
The client starts the animation and listens for the "Hit" event, then notifies the server.
The server, upon receiving this confirmation, executes the hit effect or further processing.
Code Example
Server Script (in ServerScriptService)
local CombatEvent = game.ReplicatedStorage:WaitForChild("CombatEvent")
-- Function to handle attack initiation
CombatEvent.OnServerEvent:Connect(function(player, action)
if action == "StartAttack" then
-- Validate or process initial attack logic here
print(player.Name .. " initiated an attack.")
-- Tell the client to start the attack animation
CombatEvent:FireClient(player, "PlayAttackAnimation")
elseif action == "AnimationHitEvent" then
-- This fires when the client signals that the animation's "Hit" event was reached
print(player.Name .. "'s attack hit at the correct animation frame.")
-- Perform hit detection, effects, and damage calculation here
-- Example: create hit effects and apply damage
-- ApplyDamage(player, target)
end
end)
LocalScript (in StarterPlayerScripts or a Tool)
local CombatEvent = game.ReplicatedStorage:WaitForChild("CombatEvent")
local player = game.Players.LocalPlayer
-- Sample function to initiate an attack (could be triggered by input)
local function initiateAttack()
-- Signal to the server to start the attack process
CombatEvent:FireServer("StartAttack")
end
-- Listen for the server's instruction to play the attack animation
CombatEvent.OnClientEvent:Connect(function(action)
if action == "PlayAttackAnimation" then
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- Load and play the attack animation
local attackAnimation = Instance.new("Animation")
attackAnimation.AnimationId = "rbxassetid://YourAnimationID" -- Replace with actual animation ID
local attackTrack = animator:LoadAnimation(attackAnimation)
-- Set up a listener for the "Hit" event in the animation
attackTrack:GetMarkerReachedSignal("Hit"):Connect(function()
-- Notify the server that the hit event was reached
CombatEvent:FireServer("AnimationHitEvent")
end)
-- Play the animation
attackTrack:Play()
end
end)
-- Example binding to initiate attack (e.g., when pressing a key)
game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
initiateAttack()
end
end)
Explanation
Attack Start: When the player presses a key (E in this case), initiateAttack() fires CombatEvent to the server, signaling the start of an attack.
Server Processes and Sends Animation Command: The server receives the “StartAttack” action, does any necessary setup or validation, and sends a command back to the client to play the animation.
Client Plays Animation and Listens for Event: The client receives the command to play the animation, then connects a listener to the "Hit" marker in the animation.
Hit Event Notification: When the "Hit" marker is reached in the animation, the client notifies the server with "AnimationHitEvent".
Server Handles Hit Effect: Upon receiving "AnimationHitEvent", the server applies the hit effect and damage, ensuring everything is synchronized with the animation timing.
This approach keeps processing on the server but allows you to sync with client-side animation events efficiently.
However, in the end, I AM using server-side animations.
I told you the solution. Only way is to use remote events if you want to run animations on client from the server. I just made chat gpt write out my solution. I double-checked the information as well. lmk if you have a problem