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

After Clicked Several Times The Sword Works, Why Do I Get This Error?

Asked by 7 years ago

Though the sword works, eventually after I click three times I get this error once and never again, because the sword works after that. Why do I keep getting this error even though the sword works?

.SwordScript:48: attempt to index global 'hitHumanoids' (a nil value)

Here is the script:

-- Made by Prime Alphinex

tool = script.Parent

local event = tool:WaitForChild("RemoteEvent")
local handle = tool:WaitForChild("Handle")
local debris = game:GetService("Debris")

local slashSound = handle:WaitForChild("SlashSound")
local overheadSound = handle:WaitForChild("OverheadSound")
local lungeSound = handle:WaitForChild("LungeSound")

local speedBoost = 1.25
local damage = 19
local swingTime = 1
local comboWindow = 0.5
local lastClick = tick()
local combo = 1

tool.Activated:connect(function()
    local clickDelta = tick() - lastClick
    if clickDelta > swingTime then
        hitHumanoids = {}
        lastClick = tick()
        if clickDelta < swingTime + comboWindow then
            combo = (combo + 1) % 4
        else
            combo = 1
        end
        if player then
            if combo == 1 then
                event:FireClient(player, "RunAnim", "SlashAnim2")
                slashSound:Play()
            elseif combo == 2 then
                event:FireClient(player, "RunAnim", "ThrustAnim2")
                overheadSound:Play()
            elseif combo == 3 then
                event:FireClient(player, "RunAnim", "OverheadAnim2")
                lungeSound:Play()
            end
        end
    end
end)

handle.Touched:connect(function(hit)
    if equipped and character and humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(character) then
        local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
        if targetHumanoid and targetHumanoid.health > 0 and not hitHumanoids[targetHumanoid] then
            hitHumanoids[targetHumanoid] = true

            for _, v in pairs(targetHumanoid:GetChildren()) do
                if v and v.Name == "creator" then
                    v:Destroy()
                end
            end
            local tag = Instance.new("ObjectValue")
            tag.Name = "creator"
            tag.Value = player
            debris:AddItem(tag, 3)
            tag.Parent = targetHumanoid

            targetHumanoid:TakeDamage(damage*combo)
        end
    end

end)

tool.Equipped:connect(function()
    equipped = true
    lastClick = tick()
    combo = 1
    character = tool.Parent
    player = game.Players:GetPlayerFromCharacter(character)
    humanoid = character:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.WalkSpeed = humanoid.WalkSpeed * speedBoost
    else
        character = nil
    end
end)

tool.Unequipped:connect(function()
    local equipped = false
    if humanoid then
        humanoid.WalkSpeed = humanoid.WalkSpeed / speedBoost
    end
    character = nil
    humanoid = nil
end)

1 answer

Log in to vote
0
Answered by 7 years ago

You have declared hitHumanoids inside a function that you've connected to tool.Activated, but nowhere else. The function connected to handle.Touched cannot see this variable, as it is declared only in tool.Activated.

Consider declaring hitHumanoids only where you need it, which in this case seems to be the function connected to handle.Touched.

handle.Touched:connect(function(hit)
    hitHumanoids = {}
    if equipped and character and humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(character) then
        local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
        if targetHumanoid and targetHumanoid.health > 0 and not hitHumanoids[targetHumanoid] then
            hitHumanoids[targetHumanoid] = true

            for _, v in pairs(targetHumanoid:GetChildren()) do
                if v and v.Name == "creator" then
                    v:Destroy()
                end
            end

-- ...
0
I did that already, the problem with doing that is now even if the sword is not fired/clicked, it damages the opponent, and instantly dies on contact. PrimeAlphinex 36 — 7y
0
Then check if the mouse button is clicked. NotInventedHere 158 — 7y
0
How do I do that? That seems good. PrimeAlphinex 36 — 7y
0
A simple variable would work. Have a variable that is called "mouseClicked" or something, set it to false initially, then set it to true when the mouse is clicked. Check whether it's true in the Touched script. NotInventedHere 158 — 7y
Ad

Answer this question