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

Why is my grenade not working on its second throw?

Asked by 7 years ago
Edited 7 years ago

So, at the moment I am trying to work on a grenade. It works on the first throw but when I click it again it comes up with the error: The parent BodyVelocity is locked, current parent: NULL, new parent grenade.

Heres the script:

local player = script.Parent.Parent
local mouse = player:GetMouse()
HoldingArm = player:FindFirstChild('RightArm')
Explosion = game.ServerStorage.Explosion
Explosion1 = game.ServerStorage.Explosion.Explosion1
Explosion2 = game.ServerStorage.Explosion.Explosion2
Explosion3 = game.ServerStorage.Explosion.Explosion3
Explosion4 = game.ServerStorage.Explosion.Explosion4
Explosion5 = game.ServerStorage.Explosion.Explosion5
Explosion6 = game.ServerStorage.Explosion.Explosion6
Explosion7 = game.ServerStorage.Explosion.Explosion7
Explosion8 = game.ServerStorage.Explosion.Explosion8
CanThrow = true
Grenade = game.ReplicatedStorage.Grenade:clone() 
Bv = Instance.new('BodyVelocity')
b = 1

repeat wait(0.001) until player.Character

Anim = player.Character.Humanoid:LoadAnimation(script.Animation)

mouse.KeyDown:connect(function(key)
    if CanThrow == false then return end
    if key == "e" then
        if b > 5 then return end
        CanThrow = false
        print('Grenade about to throw')
        Grenade.Parent = workspace
        Anim:Play()
        wait(0.75)
        Grenade.CFrame = player.Character.Torso.CFrame*CFrame.new(0,0,0)
        Bv.Parent = Grenade
        Bv.MaxForce = Vector3.new(1e8,1e8,1e8)
        Bv.Velocity = player.Character.Torso.CFrame.lookVector*60
        print('Grenade Thrown')
        game.Debris:AddItem(Grenade,2)
        CanThrow = true
        b = b + 1
    end
end)

Please help!

PS: There is a grenade in the ServerStorage and the script is local in the starterpack. Also the animation is in the script

1 answer

Log in to vote
0
Answered by 7 years ago

You only make one grenade:

Grenade = game.ReplicatedStorage.Grenade:clone()

You should make a new grenade every time they attempt to throw a grenade.

Also, you shouldn't use KeyDown because it's deprecated.

Also, use your variables. You made a variable Explosion then don't use it.

Use Local Variables.

Last thing is Local Scripts can't access ServerStorage, so you should move everything to ReplicatedStorage for this.

local player = game.Players.LocalPlayer --\\ Better
local mouse = player:GetMouse()
local HoldingArm = player:FindFirstChild('RightArm')
local Explosion = game.ReplicatedStorage.Explosion
local Explosion1 = Explosion .Explosion1
local Explosion2 = Explosion .Explosion2
local Explosion3 = Explosion .Explosion3
local Explosion4 = Explosion .Explosion4
local Explosion5 = Explosion .Explosion5
local Explosion6 = Explosion .Explosion6
local Explosion7 = Explosion .Explosion7
local Explosion8 = Explosion .Explosion8
local CanThrow = true
local b = 1

repeat wait(0.001) until player.Character

Anim = player.Character.Humanoid:LoadAnimation(script.Animation)

game:GetService("UserInputService").InputBegan:Connect(function(inputObject,GPE)
    if CanThrow == false then return end
    --\\ Using UserInputService.
    if not GPE and inputObject.KeyCode == Enum.KeyCode.E then
        if b > 5 then return end
        CanThrow = false
        --\\ Moved:
        local Grenade = game.ReplicatedStorage.Grenade:Clone() 
        local Bv = Instance.new('BodyVelocity')

        print('Grenade about to throw')
        Grenade.Parent = workspace
        Anim:Play()
        wait(0.75)
        Grenade.CFrame = player.Character.Torso.CFrame*CFrame.new(0,0,0)
        Bv.Parent = Grenade
        Bv.MaxForce = Vector3.new(1e8,1e8,1e8)
        Bv.Velocity = player.Character.Torso.CFrame.lookVector*60
        print('Grenade Thrown')
        game.Debris:AddItem(Grenade,2)
        CanThrow = true
        b = b + 1
    end
end)
This is only an example.

If this doesn't work, let me know any and all errors you receive and I'll try to make corrections to my code.

0
Thanks! Benified4Life 2 — 7y
0
It works :p Benified4Life 2 — 7y
Ad

Answer this question