Prevent my animation for playing on every player


So in my combat system, it seems to play the animation for everyone. only the animation. not the hitbox initiation. The combat system detects if I left click in the local script and the server script handles damage and animations:

ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("AnimationEvent")
local damageEvent = ReplicatedStorage:WaitForChild("DamageEvent")
local KnockBackConstructorModule = require(ReplicatedStorage:WaitForChild("Knockback_Module"))

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local animations = {
	script:WaitForChild("Animation1"),
	script:WaitForChild("Animation2"),
	script:WaitForChild("Animation3"),
	script:WaitForChild("Animation4")
}

local currentAnimation = 1
local debounce = false
local cooldownActive = false
local lastActionTime = tick()

local animationTracks = {}
for i, anim in ipairs(animations) do
	animationTracks[i] = humanoid:LoadAnimation(anim)
end

local function playCurrentAnimation(player)
	if debounce then return end
	debounce = true

	local currentTime = tick()
	if currentTime - lastActionTime > 1 then
		currentAnimation = 1
	end
	lastActionTime = currentTime

	local track = animationTracks[currentAnimation]
	if track then
		track:Play()

		remoteEvent:FireClient(player, track.Length, currentAnimation)
		track.Stopped:Wait()
	end

	currentAnimation = currentAnimation % #animations + 1

	if currentAnimation == 1 then
		cooldownActive = true
		wait(2)
		cooldownActive = false
	end

	debounce = false
end

remoteEvent.OnServerEvent:Connect(function(player)
	if not cooldownActive then
		playCurrentAnimation(player)
	end
end)

damageEvent.OnServerEvent:Connect(function(player, hit, humanoid, currentAnimation)
	if humanoid and humanoid:IsA("Humanoid") then
		humanoid:TakeDamage(5)

		local enemyCharacter = humanoid.Parent
		local knockbackRange = 2.5
		local knockbackHeight = 1.5

		if currentAnimation == 4 then
			knockbackRange = 8.0
			knockbackHeight = 30.0
		end

		local knockbackConstructor = KnockBackConstructorModule.new(enemyCharacter, character, knockbackRange, knockbackHeight)
		knockbackConstructor:Construct()
	end
end)

LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local remoteEvent = ReplicatedStorage:WaitForChild("AnimationEvent")
local damageEvent = ReplicatedStorage:WaitForChild("DamageEvent")
local RaycastHitbox = require(ReplicatedStorage:WaitForChild("RaycastHitboxV4"))

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {character}
Params.FilterType = Enum.RaycastFilterType.Exclude

local newHitbox = RaycastHitbox.new(character)
newHitbox.RaycastParams = Params
newHitbox.Visualizer = true

newHitbox:SetPoints(character:FindFirstChild("Left Arm"), {Vector3.new(0, 0, 0), Vector3.new(0.5, 0, 0), Vector3.new(-0.5, 0, 0), Vector3.new(0,0.5,0), Vector3.new(0,-0.5,0)})
newHitbox:SetPoints(character:FindFirstChild("Right Arm"), {Vector3.new(0, 0, 0), Vector3.new(0.5, 0, 0), Vector3.new(-0.5, 0, 0), Vector3.new(0,0.5,0), Vector3.new(0,-0.5,0)})

newHitbox.OnHit:Connect(function(hit, humanoid)
	print(hit)
	damageEvent:FireServer(hit, humanoid, newHitbox.currentAnimation)
end)

local debounce = false

local function startHitbox(duration, animationNumber)
	if debounce then return end
	debounce = true

	newHitbox.currentAnimation = animationNumber

	newHitbox:HitStart()

	wait(duration)
	newHitbox:HitStop()

	debounce = false
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
		remoteEvent:FireServer()
	end
end)

remoteEvent.OnClientEvent:Connect(function(animationLength, animationNumber)
	startHitbox(animationLength, animationNumber)
end)
1 Like

This is one of the funniest thing I have seen this month haha.

Anyways, I can tell your Script is in StarterCharacterScripts. The problem with this is that those scripts are sharing the same RemoteEvent from ReplicatedStorage. Anytime the RemoteEvent gets fired, it plays the loaded animation regardless.

1 Like

should i move the location of the script? maybe make the remote on run time?

1 Like

why cant you just move the script to server script service?
You can get the player, character and humanoid with PlayerAdded

1 Like

Whenever you fire the server, all scripts that are waiting on the “OnServerEvent” for that RemoteEvent will fire. I’d advise in your server script function “playCurrentAnimation” you add a line that double checks that the the Player that fired the RemoteEvent is equal to the player is equal to the player’s character the server script is located in.

local function playCurrentAnimation(player)
-- new stuff
if player ~= Players:GetPlayerFromCharacter(character) then
return
end
--

	if debounce then return end
	debounce = true

	local currentTime = tick()
	if currentTime - lastActionTime > 1 then
		currentAnimation = 1
	end
	lastActionTime = currentTime

	local track = animationTracks[currentAnimation]
	if track then
		track:Play()

		remoteEvent:FireClient(player, track.Length, currentAnimation)
		track.Stopped:Wait()
	end

	currentAnimation = currentAnimation % #animations + 1

	if currentAnimation == 1 then
		cooldownActive = true
		wait(2)
		cooldownActive = false
	end

	debounce = false
end
1 Like

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