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)
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 -- ...