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.
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)
What solutions have you tried so far? I searched through the devforum but can’t seem to find what I needed.
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)
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).
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