What might hit the hitbox is the player creating it and an npc or other player. Also, for some reason, any time I try and do the function again, x does not spawn. This is my full code:
local function Dash()
local character = Player.Character or Player.CharacterAdded:Wait()
if character and not dashdebounce then
dashdebounce = true
local h = character.Humanoid
local hrp = character.HumanoidRootPart
local dashanim = Instance.new("Animation")
dashanim.AnimationId = 'rbxassetid://14512159842'
local playdashanim = h:LoadAnimation(dashanim)
local dashdirection = nil
local movedirection = h.MoveDirection
local lv = hrp.CFrame.LookVector
local isOnGround = h.FloorMaterial ~= Enum.Material.Air and h.FloorMaterial ~= Enum.Material.Water
if isOnGround then
if movedirection == Vector3.new(0,0,0) then
dashdirection = hrp.Position + Vector3.new(lv.X, 0, lv.Z)
else
dashdirection = hrp.Position + Vector3.new(movedirection.X, 0, movedirection.Z)
end
local bg = Instance.new("BodyGyro")
bg.Parent = hrp
bg.D = 0
bg.P = 500000
bg.CFrame = CFrame.lookAt(hrp.Position, dashdirection)
local att = Instance.new("Attachment")
att.Parent = hrp
local vf = Instance.new("VectorForce")
vf.Parent = hrp
vf.Attachment0 = att
PlayAnim:Stop()
playhitanimation:Stop()
if UIS:IsKeyDown(Enum.KeyCode.W) then
debounce = true
playdashanim:Play()
vf.Force = Vector3.new(0,0,-20000)
wait(0.25)
vf:Destroy()
playdashanim:Stop()
local x = Instance.new("Part", workspace)
x.Name = "Hitbox"
x.BrickColor = BrickColor.new("Really red")
x.Size = Vector3.new(4, 4, 7)
x.CanCollide = false
x.Transparency = 0.5
x.Massless = true
local w = Instance.new("Weld", x)
w.Part0 = Character.HumanoidRootPart
w.Part1 = x
w.C1 = CFrame.new(0, 0, 2)
local function CheckTouched() --This is the function
for i, v in pairs(workspace:GetPartsInPart(x)) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
local x = Instance.new("IntValue", v.Parent)
x.Name = "Hit"..Player.Name
game.Debris:AddItem(x, 1)
dashevent:FireServer(v)
end
end
return false
end
while not touched do
if touched == false then
touched = CheckTouched()
end
if timer < 0 then
x:Destroy()
break
end
timer = timer - task.wait(0.1)
end
elseif UIS:IsKeyDown(Enum.KeyCode.S) then
vf.Force = Vector3.new(0,0,20000)
wait(0.2)
vf.Force = Vector3.new(0,0,-1000)
wait(0.05)
vf:Destroy()
bg:Destroy()
att:Destroy()
elseif UIS:IsKeyDown(Enum.KeyCode.D) then
local rightdashanim = Instance.new("Animation")
rightdashanim.AnimationId = 'rbxassetid://14564814060'
local playrightdashanim = Character.Humanoid:LoadAnimation(rightdashanim)
playrightdashanim:Play()
vf.Force = Vector3.new(20000,0,0)
wait(0.2)
vf.Force = Vector3.new(-1000,0,0)
wait(0.05)
playrightdashanim:Stop()
vf:Destroy()
bg:Destroy()
att:Destroy()
elseif UIS:IsKeyDown(Enum.KeyCode.A) then
local leftdashanim = Instance.new("Animation")
leftdashanim.AnimationId = 'rbxassetid://14565367605'
local playleftdashanim = Character.Humanoid:LoadAnimation(leftdashanim)
playleftdashanim:Play()
vf.Force = Vector3.new(-20000,0,0)
wait(0.2)
vf.Force = Vector3.new(1000,0,0)
wait(0.05)
playleftdashanim:Stop()
vf:Destroy()
bg:Destroy()
att:Destroy()
else
debounce = true
playdashanim:Play()
vf.Force = Vector3.new(0,0,-20000)
local x = Instance.new("Part", workspace)
x.Name = "Hitbox"
x.BrickColor = BrickColor.new("Really red")
x.Size = Vector3.new(4, 4, 7)
x.CanCollide = false
x.Transparency = 0.5
x.Massless = true
local w = Instance.new("Weld", x)
w.Part0 = Character.HumanoidRootPart
w.Part1 = x
w.C1 = CFrame.new(0, 0, 2)
local function CheckTouched()
for _, v in pairs(workspace:GetPartsInPart(x)) do
local player = game.Players:GetPlayerFromCharacter(v.Parent)
if player and player~= game.Players.LocalPlayer then
local x = Instance.new("IntValue", v.Parent)
x.Name = "Hit"..Player.Name
game.Debris:AddItem(x, 1)
dashevent:FireServer(v)
return true
end
end
return false
end
while not touched do
touched = CheckTouched()
if timer < 0 then
x:Destroy()
break
end
timer = timer - task.wait(0.1)
end
vf:Destroy()
playdashanim:Stop()
end
else --Don't mind anything after this
if movedirection == Vector3.new(0,0,0) then
dashdirection = hrp.Position + Vector3.new(lv.X, 0, lv.Z)
else
dashdirection = hrp.Position + Vector3.new(movedirection.X, 0, movedirection.Z)
end
PlayAnim:Stop()
playhitanimation:Stop()
playdashanim:Play()
local bg = Instance.new("BodyGyro")
bg.Parent = hrp
bg.D = 0
bg.P = 500000
bg.CFrame = CFrame.lookAt(hrp.Position, dashdirection)
local att = Instance.new("Attachment")
att.Parent = hrp
local vf = Instance.new("VectorForce")
vf.Parent = hrp
vf.Attachment0 = att
if UIS:IsKeyDown(Enum.KeyCode.W) then
playdashanim:Play()
vf.Force = Vector3.new(0,0,-20000)
local x = Instance.new("Part", workspace)
x.Name = "Hitbox"
x.BrickColor = BrickColor.new("Really red")
x.Size = Vector3.new(4, 4, 7)
x.CanCollide = false
x.Transparency = 0.5
x.Massless = true
local w = Instance.new("Weld", x)
w.Part0 = Character.HumanoidRootPart
w.Part1 = x
w.C1 = CFrame.new(0, 0, 2)
while task.wait(0.1) do
for i, v in pairs(workspace:GetPartsInPart(x)) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
local x = Instance.new("IntValue", v.Parent)
x.Name = "Hit"..Player.Name
game.Debris:AddItem(x, 1)
dashevent:FireServer(v)
end
end
break --stops the loop
end
wait(duration)
x:Destroy()
playdashanim:Stop()
elseif UIS:IsKeyDown(Enum.KeyCode.S) then
vf.Force = Vector3.new(0,0,20000)
wait(0.2)
vf.Force = Vector3.new(0,0,-1000)
wait(0.05)
vf:Destroy()
bg:Destroy()
att:Destroy()
elseif UIS:IsKeyDown(Enum.KeyCode.D) then
vf.Force = Vector3.new(20000,0,0)
wait(0.2)
vf.Force = Vector3.new(-1000,0,0)
wait(0.05)
vf:Destroy()
bg:Destroy()
att:Destroy()
elseif UIS:IsKeyDown(Enum.KeyCode.A) then
vf.Force = Vector3.new(-20000,0,0)
wait(0.2)
vf.Force = Vector3.new(1000,0,0)
wait(0.05)
vf:Destroy()
bg:Destroy()
att:Destroy()
end
end
end
end