Hello, I got such a problem with pushing script.
So my problem is when a grenade explodes player should be pushed back, but player getting pushed side-wards.
Source:
local Explosion = script.Parent
local Damage = 8
Explosion.Hit:Connect(function(part, distance)
if not part or not part.Parent then return end
if part.Parent:FindFirstChild("Humanoid") == nil then return end
local hum = part.Parent:WaitForChild("Humanoid")
hum:TakeDamage(Damage)
local bv = Instance.new("BodyVelocity", part.Parent.Torso)
bv.Velocity = part.Parent.Torso.CFrame.LookVector * -130
game.Debris:AddItem(bv, 0.25)
end)
try applying the direction from the grenade to the player as a force of the explosion instead of hte lookvector. this is done like so:
local directionNonUnit = (grenadepos - playerpos)
local distance = directionNonUnit.magnitude
local direction = directionNonUnit.unit
bv.Velocity = direction*distance
Edit
this gives the velocity a force compared to the distance of the player and the grenade to make sure the right velocity is applied over distance
loop through the whole playerlist, check if people have a character in the workspace:
for i,v in pairs(game.Players:GetPlayers()) do
if v.Character then
local hum = part.Parent:WaitForChild("Humanoid")
hum:TakeDamage(Damage)
local bv = Instance.new("BodyVelocity", part.Parent.Torso)
local directionNonUnit = (grenadepos - playerpos)
local distance = directionNonUnit.magnitude
local direction = directionNonUnit.unit
bv.Velocity = direction*distance
end
end
basically apply what i mentioned above into your scrip and you should be fine with the force being applied correctly.
edit
this kind of depends on the system you have
Edited :GetChildren() to :GetPlayers() i usually automatically write :GetChildren() thus i made a swiftness mistake
A little note when getting Players; the canonical function to use here is GetPlayers, which explicitly returns instances of the Player class. You typically won’t have anything other than Players in the Players service though.