Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone help me with removing my grenade after throwing it?

Asked by 6 years ago

i have a grenade... and when i throw it it can reload. but i want to make it more realistic so i trought i can add script.Parent.Parent:remove(). but, idk where to place it! i tryed it myself but i didnt work because idk where to place it. can you please help? here's the script.

local tool = script.Parent
local config = tool:WaitForChild("Configurations")


function AngleOfReach(distance, altitude, velocity)
    local theta = math.atan((velocity^2 + math.sqrt(velocity^4 -196.2*(196.2*distance^2 + 2*altitude*velocity^2)))/(196.2*distance))
    if theta ~= theta then
        theta = math.pi/4
    end
    return(theta)
end


function Explode(part)
    local explosion = Instance.new("Explosion", workspace)
    explosion.Position = part.Position
    explosion.BlastRadius = config.ExplosionRadius.Value
    part:Destroy()
end

script.Parent.Throw.OnServerEvent:connect(function(player, mousePosition)
    local handlePos = Vector3.new(tool.Handle.Position.X, 0, tool.Handle.Position.Z) 
    local mousePos = Vector3.new(mousePosition.X, 0, mousePosition.Z) 
    local distance = (handlePos - mousePos).magnitude 
    local altitude = mousePosition.Y - tool.Handle.Position.Y
    local angle = AngleOfReach(distance, altitude, config.GrenadeVelocity.Value) 

    tool.Handle.Transparency = 1
    local grenade = tool.Handle:Clone()
    grenade.Parent = workspace
    grenade.Transparency = 0
    grenade.CanCollide = true
    grenade.CFrame = tool.Handle.CFrame
    grenade.Velocity = (CFrame.new(grenade.Position, Vector3.new(mousePosition.X, grenade.Position.Y, mousePosition.Z)) * CFrame.Angles(angle, 0, 0)).lookVector * config.GrenadeVelocity.Value -- Throwing 'n stuff, it probably didn't need to be this long
    spawn(function()
    if config.ExplodeOnTouch.Value then
        grenade.Touched:connect(function(hit)
            if hit.Parent ~= tool.Parent and hit.CanCollide then 
                Explode(grenade)
            end
        end)
    else

            wait(config.FuseTime.Value)
            Explode(grenade)
    end
    end)
    wait(config.Cooldown.Value)
    tool.Handle.Transparency = 0
end)
0
Making this a comment, because it might not work, but maybe adding a `wait(TimeAmount)` then `grenade:Destroy()`? Obviously replace "grenade" with something like `game.Workspace:FindFirstChild("Grenade")` JellyYn 70 — 6y
0
try using game.Debris:AddItem(grenade,1--number for how long til destroyed) Instead of :Destroy(). Nikkulaos 229 — 6y

Answer this question