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)

4 comments
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.
Then check if the mouse button is clicked.
How do I do that? That seems good.
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.