How to Know who Hit First

I would like to make a game where players run into each other and the one who hits first gets knocked back.

LINK TO GAMEPLAY: https://youtu.be/vu2DIqEGUhE
this is a model i created the same way it would work if a player joined but when another player is in the game it acts completely different and they usually end up sending each other back.

I cant figure out a way to tell who hits first because when players run into each other they both hit each other

I attempted to use a Boolean that turns on when a player is hit that makes it so they cant be hit again but this doesn’t seem to work. I also tried to use the players ticks and compare them then see who hit first but this seemed to work even worse.

LOCAL SCRIPT

local knockbackEvent = ReplicatedStorage:WaitForChild("KnockbackEvent")

local character = script.Parent
local upperTorso = character:WaitForChild("UpperTorso")
local debounce = false

upperTorso.Touched:Connect(function(hit)
	if debounce or hit == upperTorso then return end
	debounce = true
	print("Touch detected. Sending event.")

	local hitCharacter = hit.Parent
	local humanoid = hitCharacter:FindFirstChildOfClass("Humanoid")

	if humanoid and hitCharacter ~= character then
		local currentTime = tick()
		local velocity = character:FindFirstChild("HumanoidRootPart").Velocity
		knockbackEvent:FireServer(hitCharacter, character, currentTime, velocity)
		print("Event fired with:", hitCharacter.Name, character.Name, currentTime, velocity)
	end

	task.wait(0.1)
	debounce = false
end)

type or paste code here

SERVER SCRIPT: `local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local knockbackEvent = ReplicatedStorage:WaitForChild(“KnockbackEvent”)

local lastTouchTimes = {}

local function flyPlayerBackwards(hitCharacter, triggeringCharacter)
local humanoidRootPart = hitCharacter:FindFirstChild(“HumanoidRootPart”)
local knockback = hitCharacter.DeathPercent.Value
if not humanoidRootPart then
print(“Error: humanoidRootPart not found for hitCharacter:”, hitCharacter.Name)
return
end

print("Applying knockback to:", hitCharacter.Name, "Triggered by:", triggeringCharacter.Name)

local direction = (humanoidRootPart.Position - triggeringCharacter.HumanoidRootPart.Position).unit
local pushBack = Instance.new("LinearVelocity")
local finalVector = direction * knockback + Vector3.new(0, 12, 0) -- Adjust as needed
pushBack.VectorVelocity = finalVector
pushBack.ForceLimitsEnabled = true
pushBack.MaxForce = math.huge
pushBack.RelativeTo = Enum.ActuatorRelativeTo.Attachment0

if not humanoidRootPart:FindFirstChild("Attachment0") then
	local attachment = Instance.new("Attachment")
	attachment.Name = "Attachment0"
	attachment.Parent = humanoidRootPart
	print("Created Attachment0 for:", hitCharacter.Name)
end

pushBack.Attachment0 = humanoidRootPart:FindFirstChild("Attachment0")
pushBack.Parent = humanoidRootPart
pushBack.Enabled = true
print("LinearVelocity applied:", pushBack.VectorVelocity)

task.wait(0.2)
pushBack:Destroy()
hitCharacter.DeathPercent.Value = hitCharacter.DeathPercent.Value + 20
print("Knockback applied successfully to:", hitCharacter.Name)

end

knockbackEvent.OnServerEvent:Connect(function(player, hitCharacter, triggeringCharacter, currentTime, velocity)
print(“Event received with:”, player.Name, hitCharacter.Name, triggeringCharacter.Name, currentTime, velocity)
local playerName = player.Name
lastTouchTimes[playerName] = currentTime

flyPlayerBackwards(hitCharacter, triggeringCharacter)

end)
`

Yes the code was written with the help of ai.

MODEL OF PLAYER:
image

This wouldn’t work, because the touched event should fire at the same time. I don’t think the touch event fires first for the moving part, and then, the part that was touched, it just fires all events that are connected at the same time. You would need to check the direction both players are going, and the direction that is more pointed toward the other player would be the one that “touched”

Do you know how I check what angle the player would be looking at.

I’m not sure about that, but you could find the player’s move direction from the humanoid, add that and the player’s position, and which ever is closest to the other player would be the one that touched.

Here is an image


The red represents players. The black is the direction he is moving.
The green is the player’s position + move direction.
You can see that the distance to player1 to player2 is 3, and the distance to player2 to player 1 is 10 (it would actually be around 4). Therefore, player 1 is the one who would be considered “touching”, because he is moving towards the other, and his distance is the least.

Thanks so much this helped a TON

1 Like