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: