Damage System bug... AGAIN

Hello again! thanks to everyone who helped me on the last code. Well, I took the stepping code and turned it into a server code, so that it works for all players, however, the code came with several failures: Movement did not stop and the animation did not happen. Damage is being applied to the object after I hit C, but otherwise nothing works. How can I resolve this?

Code when it was LocalScript:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local pisarEnabled = false -- Variable to control if the stomping is enabled

local kickAnimationId = "13874028312" -- Kick animation ID

local damageAmount = 100 -- Damage value to be applied

-- Function to apply damage to the object
local function ApplyDamageToObject(object)
	local vidaDeObjetos = object:FindFirstChild("Vida de objetos")
	if vidaDeObjetos then
		ReplicatedStorage:WaitForChild("DamageServer"):FireServer(vidaDeObjetos, damageAmount) -- Sends the damage signal to the object with the damage value
	end
end

-- Function to play the kick animation
local function PlayAnimation(animationId)
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://" .. animationId

	local track = humanoid:LoadAnimation(animation)
	track:Play()

	return track
end

-- Function to toggle the stomping feature
local function TogglePisar()
	if not pisarEnabled then
		-- Enable stomping
		pisarEnabled = true
		humanoid.WalkSpeed = 0 -- Set the walk speed to 0
		local kickAnimationTrack = PlayAnimation(kickAnimationId) -- Play the kick animation
		kickAnimationTrack.Stopped:Connect(function()
			pisarEnabled = false -- Disable stomping after the animation finishes
			humanoid.WalkSpeed = 16 -- Restore the default walk speed
		end)
	end
end

-- Connect the toggle function to the C key input event
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.C then
		TogglePisar()
	end
end)

-- Connect the damage function to triggering when the right foot touches an object only when stomping is enabled and the C key is pressed
local rightFoot = character:WaitForChild("RightFoot")
rightFoot.Touched:Connect(function(part)
	if pisarEnabled and UserInputService:IsKeyDown(Enum.KeyCode.C) then
		local object = part.Parent
		ApplyDamageToObject(object) -- Apply damage to the touched object
	end
end)

The new code for server:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local kickAnimationId = "13874028312" -- Kick animation ID
local damageAmount = 100 -- Damage value to be applied
local movementDelay = 1.5 -- Delay in seconds to disable movement after button press

-- Function to apply damage to the object
local function ApplyDamageToObject(object)
	local vidaDeObjetos = object:FindFirstChild("Vida de objetos")
	if vidaDeObjetos then
		ReplicatedStorage:WaitForChild("DamageServer"):FireServer(vidaDeObjetos, damageAmount) -- Sends the damage signal to the object with the damage value
	end
end

-- Function to play the kick animation
local function PlayAnimation(animationId, humanoid)
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://" .. animationId

	local track = humanoid:LoadAnimation(animation)
	track:Play()

	return track
end

-- Function to disable movement for a given humanoid
local function DisableMovement(humanoid)
	humanoid:ChangeState(Enum.HumanoidStateType.Physics) -- Change to physics state to disable movement
	wait(movementDelay) -- Wait for the specified delay
	humanoid:ChangeState(Enum.HumanoidStateType.GettingUp) -- Change to getting up state to re-enable movement
end

-- Function to toggle the stomping feature for a player
local function TogglePisar(player)
	local character = player.Character
	local humanoid = character and character:FindFirstChild("Humanoid")
	if not humanoid then
		return
	end

	local pisarEnabled = false -- Variable to control if the stomping is enabled
	local kickAnimationTrack = nil -- Track for the kick animation

	-- Connect the damage function to triggering when the right foot touches an object only when stomping is enabled
	local function OnRightFootTouched(part)
		if pisarEnabled then
			local object = part.Parent
			ApplyDamageToObject(object) -- Apply damage to the touched object
		end
	end

	-- Connect the toggle function to the C key input event
	local function OnInputBegan(input, gameProcessedEvent)
		if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.C then
			pisarEnabled = not pisarEnabled -- Toggle the stomping state
			if pisarEnabled then
				kickAnimationTrack = PlayAnimation(kickAnimationId, humanoid) -- Play the kick animation
				DisableMovement(humanoid) -- Disable movement for the specified delay
			else
				if kickAnimationTrack then
					kickAnimationTrack:Stop() -- Stop the kick animation if it is playing
				end
			end
		end
	end

	-- Connect the damage function to the right foot touch event
	local rightFoot = character:WaitForChild("RightFoot")
	rightFoot.Touched:Connect(OnRightFootTouched)

	-- Connect input event for the player
	player:GetMouse().KeyDown:Connect(OnInputBegan)
end

-- Function to handle player added event
local function OnPlayerAdded(player)
	TogglePisar(player)
end

-- Handle player added event for existing players
for _, player in ipairs(Players:GetPlayers()) do
	OnPlayerAdded(player)
end

-- Handle player added event for future players
Players.PlayerAdded:Connect(OnPlayerAdded)

1 Like

From what I hear Animations are only run locally, so they don’t work in a server script.

1 Like

would it be possible for me to run it for all players?

KeyDown returns a string instead of an Enum value.

I don’t understand what your trying to accomplish since I didn’t read the other post, but I think this will help.

Edit: Also, KeyDown doesnt return a GameProceseed value.

1 Like

I will try, thank you very much.