I can’t find a more IN DEPTH video on this so I’m really confused right now. This code is from a video that when a gun is fired, it knocks back someone. I put some explanations in next to lines I THINK I understand, so if you want to correct me on any of them, please do. Also, the code works but I would like someone to explain more what’s happening here and HOW it works, specifically the knockback function. Thanks!
local re = script.Parent:WaitForChild("RemoteEvent")
local damage = 20
local bang = script.Parent.pistol.GunShot
--Velocity and angular force
local function knockback(plrChar, enemyChar)
if plrChar and enemyChar then
local pHumRoot = plrChar:FindFirstChild("HumanoidRootPart")
local eHumRoot = enemyChar:FindFirstChild("HumanoidRootPart")
if plrChar and enemyChar then
local dir = (eHumRoot.Position - pHumRoot.Position).Unit --Finds direction
local attachment = Instance.new("Attachment", eHumRoot)--Puts attachment in enemy's humanoid root part
local force = Instance.new("VectorForce", enemyChar)--Puts Vector force in enemy's character
1. force.Attachment0 = attachment -- Attaches Vector force to the humanoid root part
2. force.Force = (dir + Vector3.new(0, 1, 0)).Unit * 10000 -- where to force it to
3. force.RelativeTo = Enum.ActuatorRelativeTo.World
enemyChar.Humanoid.PlatformStand = true
local rotate = Instance.new("AngularVelocity", eHumRoot)--Places angular velocity instance in enemy's humanoid root part
1. rotate.Attachment0 = attachment --makes angular velocity attached to its own parent
4. rotate.AngularVelocity = Vector3.new(1, 1, 1) * 30
5. rotate.MaxTorque = math.huge
3. rotate.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
game.Debris:AddItem(force, .2)
game.Debris:AddItem(rotate, .2)
game.Debris:AddItem(attachment, .2)
wait(2)
enemyChar.Humanoid.PlatformStand = false
end
end
end
local function onShoot(player, target)
bang:Play()
if target and target.Parent then
local hum = target.Parent:FindFirstChild("Humanoid")
if hum then
hum:TakeDamage(damage)
knockback(player.Character, target.Parent)
end
end
end
re.OnServerEvent:Connect(onShoot)
Key:
1.Why do the forces(Vector and Angular) attach to their own parent? Is the object just put there just so that it has a parent, or is it there to affect the attachment?
2.I understand this lines because the force goes in that direction. But is the .Unit part necessary? And why does it multiply by 10000?
3.I just don’t understand these
4. What exactly do the numbers in Vector3.new do? And why is it multiplied by 30???
5. I’ve been told this is just always math.huge, but I’ve also been told that it does matter, but how, and what does it do?
I understand the meaning of vector. But what does Angular force do? I tested out what happens without angular force, and it just makes the shot player go flying in pretty much a straight line. I thought it was probably just the 10000, but when I added angular force, it seemed like the strength of the vector go weaker.