Punch Script Does Not Function Properly

  1. What do you want to achieve? I have a punch script where if you click a MouseButton1 or tap the screen on mobile, it will fire a remoteEvent which will play a punching animation. However, when it comes to a .Touched event, it seems to always fire even when the remote is not firing and also the script seems to not be able to :GetPlayerFromCharacter(Hit.Parent) because even though I hit a player, it said that the object I hit is not a player.

  2. What is the issue? As I said in the “What do you want to achieve?” section, .Touched event fires at all time and the script cannot find the player even though I hit a player.

Here is the server script:

game.ReplicatedStorage.RemoteEvents.Punch.OnServerEvent:Connect(function(Player, Animation)
	Player.Character:FindFirstChild("Humanoid"):LoadAnimation(Animation):Play()
	Player.Character:FindFirstChild("LeftHand").Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			if game.Players:GetPlayerFromCharacter(Hit.Parent) and game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit == false then
				game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit = true
				Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
				wait(1)
				game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit = false
			end	
		end
	end)
end)
  1. What solutions have you tried so far? I searched through the devforum but can’t seem to find what I needed.

You need to disconnect the function after the animation finishes playing.

2 Likes

Try this

game.ReplicatedStorage.RemoteEvents.Punch.OnServerEvent:Connect(function(Player, Animation)
	local animation = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(Animation)
	animation:Play()
	local hitConnection = Player.Character:FindFirstChild("LeftHand").Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			if game.Players:GetPlayerFromCharacter(Hit.Parent) and game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit == false then
				game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit = true
				Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
				wait(1)
				game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit = false
			end	
		end
	end)
	animation.Stopped:Connect(function()
		hitConnection:Disconnect()
	end)
end)
1 Like

Thanks, the touched event now worked. But the script still couldn’t get the player from character even though I hit the player(It was very confusing for me since it usually works).

wait(1)
				game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit = false

This part doesn’t have time to run, try

delay(1 , function()
	game.Players:GetPlayerFromCharacter(Hit.Parent).AlreadyHit = false
end

Doesn’t work, I tried to check by doing

if game.Players:GetPlayerFromCharacter(Hit.Parent) then
    print("Is A Player")
else
    print("Not A Player")
end

but it always print “Not A Player” even if I hit a player.

Store the player as a variable since after disconnecting, the “hit” will not exist.

local PlayerHit
if game.Players:GetPlayerFromCharacter(Hit.Parent) then
	PlayerHit = game.Players:GetPlayerFromCharacter(Hit.Parent)
end
if Hit.Parent:FindFirstChild("Humanoid") then
	if PlayerHit and PlayerHit.AlreadyHit == false then
		PlayerHit.AlreadyHit = true
		Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
		wait(1)
		delay(1 , function()
			PlayerHit.AlreadyHit = false
		end)
	end	
end

I tried this and it still doesn’t work.

Remove the “wait(1)”

the delay replaces it.

Nevermind, I figured it out. I forgot to put .Value after the AlreadyHit.

2 Likes