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

Backpack.Sword.SwordScript:20: attempt to index global 'hithumanoid' (a nil value) error, help?

Asked by 7 years ago

I get this error: 20:10:13.193 - Players.Player1.Backpack.Sword.SwordScript:20: attempt to index global 'hithumanoid' (a nil value)

Script:

tool = script.Parent
local handle = tool:WaitForChild("Handle")
local event = tool:WaitForChild("RemoteEvent")
local speedboost = 1.25
local damage = 50
local swingtime = 1
local lastclick = tick()
local combowindow = .5
local combo = 0
local SlashSound = handle:WaitForChild("SlashSound")
local OverheadSound = handle:WaitForChild("OverheadSound")
local LungeSound = handle:WaitForChild("LungeSound")

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 hithumanoid[targethumanoid] then
            hithumanoids[targethumanoid] = true
            targethumanoid:TakeDamage(damage * combo)
        end
        end
end)

tool.Activated:connect(function()
    local clickdelta = tick() - lastclick
    if clickdelta > swingtime then
    lastclick = tick()
    local hithumanoids = {}
    if clickdelta < slashtime + combowindow then
        combo = combo + 1 % 3
        else combo = 0
    end
    if player then
        if combo == 0 then 
            event:FireClient(player, "RunAnimation", "SlashAnim2")
            SlashSound:Play()
        elseif combo == 1 then
            event:FireClient(player, "RunAnimation", "ThrustAnim2")
            OverheadSound:Play()
        elseif combo == 2 then
        event:FireClient(player, "RunAnimation", "OverheadAnim2")
        LungeSound:Play()
        end
    end
    end
    end)

tool.Equipped:connect(function()
    equipped = true
    lastclick = tick()
    combo = 0
    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()
    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

It's the wrong variable: it should be hithumanoids, not hithumanoid (line 17 in the script you've provided).

Ad

Answer this question