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

Why is this getting an error?

Asked by 8 years ago
Equipped = false
canHit = false
Damage = nil
MoveNum = 1
Moves = script.Parent.Moves
canActivate = false
Handle = script.Parent.Handle
allLoaded = false
PushBack = true
AmmoCap = 6
ReloadTime = 2
Cooltime = 0.5

function Load()
    --ANIMATIONS--
    script.Parent.Moves:WaitForChild("1")
    script.Parent.Moves:WaitForChild("2")
    script.Parent.Moves:WaitForChild("Reload")
    script.Parent.Moves:WaitForChild("Prepare")
    script.Parent.Moves:WaitForChild("Idle")
    script.Parent.Moves:WaitForChild("Equip")

    loadM1 = Moves[1]       
    loadM2 = Moves[2]           
    loadM3 = Moves.Reload
    loadM4 = Moves.Prepare
    loadIdle = Moves.Idle
    loadEquip = Moves.Equip

    M1 = Humanoid:LoadAnimation(loadM1)
    M2 = Humanoid:LoadAnimation(loadM2)
    Reload = Humanoid:LoadAnimation(loadM3)
    Prepare = Humanoid:LoadAnimation(loadM4)
    IdleAnim = Humanoid:LoadAnimation(loadIdle)
    EquipAnim = Humanoid:LoadAnimation(loadEquip)


    --SOUNDS--
    Sounds = script.Parent.Sounds
    Sounds:WaitForChild("1")
    Sounds:WaitForChild("2")
    Sounds:WaitForChild("Reload")
    Sounds:WaitForChild("Prepare")
    Sounds:WaitForChild("Idle")
    Sounds:WaitForChild("Equip")

    --EFFECTS--
    SmallExplosion = game.Lighting:WaitForChild("SmallExplosion")
    SBoom = SmallExplosion:Clone()


    S1 = Sounds["1"]
    S2 = Sounds["2"]
    Reload = Sounds.Reload
    Prepare = Sounds.Prepare
    IdleSound = Sounds.Idle
    EquipSound = Sounds.Equip

    --OTHER--
    Torso = game.Players.LocalPlayer.Character:WaitForChild("Torso")
    local function Start()
        IdleSound:Play()
        IdleAnim:Play()
    end
    EquipAnim:Play()
    EquipSound:Play()
    EquipSound.Ended:connect(Start)
    allLoaded = true
end

--------------------
function Move1()
    Damage = 10
    canHit = true
    canActivate = false
    print("Move1")
    M1:Play()
    S1:Play()
    wait(1)
    canActivate = true
    canHit = false
    IdleAnim:Play()
end


function Move2()
    Damage = 10
    canHit = true
    canActivate = false
    print("Move2")
    M2:Play()
    S2:Play()
    wait(1)
    canActivate = true
    canHit = false
    IdleAnim:Play()

end


function Reload()
    print("reloading")
end


function Prepare()
    print("prepared")
end
--------------------
function onEquipped()
    Equipped = true
    canHit = true
    Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
    print("Equipped")
    Load()
    canActivate = true
end

function unEquipped()
    if allLoaded == true then
        IdleAnim:Stop()
        IdleSound:Stop()
        EquipAnim:Stop()
    end
end
--------------------

function onTouched(Hit)
    if canHit == true then
        if Hit.Parent ~= script.Parent then
            if Hit.Parent ~= Humanoid.Parent then
                otherTorso = Hit.Parent:WaitForChild("Torso")
                local hitHumanoid = Hit.Parent:WaitForChild("Humanoid")
                if hitHumanoid then
                    hitHumanoid:TakeDamage(Damage)
                    canHit = false
                end
                if PushBack == true then
                    local BodyVelocity = Instance.new("BodyVelocity")
                    BodyVelocity.Name = "PushBack"
                    BodyVelocity.P = 2000
                    BodyVelocity.maxForce = Vector3.new(BodyVelocity.P, BodyVelocity.P, BodyVelocity.P)
                    BodyVelocity.velocity = (otherTorso.Position - Torso.Position).unit * 50
                    BodyVelocity.Parent = otherTorso

                    local ChatService = game:GetService("Chat")
                    local dmgIndi = Instance.new("Part", game.Workspace)
                    dmgIndi.Anchored = true
                    dmgIndi.Transparency = 1
                    dmgIndi.CanCollide = false
                    dmgIndi.Position = Vector3.new(otherTorso.Position.X, otherTorso.Position.Y+3, otherTorso.Position.Z)
                    ChatService:Chat(dmgIndi, tostring(Damage), "Red")

                    wait(0.5)
                    BodyVelocity:Destroy()

                    wait(0.5)
                    dmgIndi:Destroy()
                else
                    local ChatService = game:GetService("Chat")
                    local dmgIndi = Instance.new("Part", game.Workspace)
                    dmgIndi.Anchored = true
                    dmgIndi.Transparency = 1
                    dmgIndi.CanCollide = false
                    dmgIndi.Position = Vector3.new(otherTorso.Position.X, otherTorso.Position.Y+3, otherTorso.Position.Z)
                    ChatService:Chat(dmgIndi, tostring(Damage), "Red")

                    wait(1)
                    dmgIndi:Destroy()
                end
            end
        end
    end
end


function Activated()
    if canActivate == true then
        if MoveNum == 1 then
            MoveNum = MoveNum + 1
            Move1()
        elseif MoveNum == 2 then
            MoveNum = 1
            Move2()
        end
    end
end

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
        Reload()                                --I get an error here
    elseif 
        inputObject.KeyCode == Enum.KeyCode.E then
        Prepare()                               --and here.
    end
end

script.Parent.Handle.Touched:connect(onTouched)
script.Parent.Activated:connect(Activated)
script.Parent.Equipped:connect(onEquipped)
script.Parent.Unequipped:connect(unEquipped)
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

It doesn't run the function. Actually, the script is in a tool and I set it up when the user presses a certain button on the keyboard it runs the function. It works..but not when I have the tool equipped. Which is strange. Help?

Answer this question