Hello folks, I’m working on a script for a mine, where when the player steps on the mine, it explodes. However, I want the mine to throw the player away to make it even better. Can you help me?
local vfx = script.Parent:FindFirstChild("VFX")
local hasExploded = false
function ActivateVFX()
if vfx then
for _, child in ipairs(vfx:GetChildren()) do
if child:IsA("ParticleEmitter") then
child:Emit(20)
end
end
end
end
function OnTouch(hit)
if hasExploded then return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid:IsA("Humanoid") and hasExploded == false then
hasExploded = true
script.Parent.Click:Play()
wait(0.2)
script.Parent.ExplosionSound:Play()
ActivateVFX()
humanoid:TakeDamage(500)
script.Parent.Transparency = 1
script.Parent.CanCollide = false
wait(20)
hasExploded = false
script.Parent.Transparency = 0
script.Parent.CanCollide = true
end
end
script.Parent.Touched:Connect(OnTouch)
What do you mean by “throw the player away”? Do you mean like literally after the explosion happens you want the player to fling or like have them get thrown into a trash can?
I am assuming that you want something like BodyVelocity! It will send the player where u set the Velocity. Only downfall is that, BodyVelocity is not recommended and is replaced by LinearVelocity. Practically the same thing, but requires an Attachment. Note: If you are using Custom Characters, make sure they got an Attachment in the PrimaryPart(If you use Linear Velocity).
After choosing one, what you wanna do is make one(Linear or Body) and put it into the Players PrimaryPart. Make sure to set the Vector/Velocity Value to the (player - mine).Unit Vector(This will give the direction to fling the player in). As well the MaxForce will need to be increased for more Power!
A script to toss the player back … inside a part (mine for you)
-- SeverScript
local bv, char
local db = true
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if db then db = false char = hit.Parent
bv = Instance.new("BodyVelocity")
bv.Parent = (char.Head)
bv.MaxForce = Vector3.new(100000,100000,100000)
bv.Velocity = (char.Head.CFrame.LookVector * -80)
+ (char.Head.CFrame.UpVector * 80)
wait(0.01) bv:Destroy()
db = true
end
end
end)