0

Is it possible to make a part that is welded to the Humanoid Root Part move with it? It serves as a hitbox, but I encounter the problem that if I move forward while attacking it hurts the player. For this I have 2 scripts Local Script:

`local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")

local events = rs:WaitForChild("Events")
local hitbox = events:WaitForChild("Hitbox")

local plr = game.Players.LocalPlayer
local character = plr.Character
if not character then
    
    plr:WaitForChild("Character")
    
end
local humanoid = character.Humanoid
local animator = humanoid:WaitForChild("Animator")

local idle = animator:LoadAnimation(script:WaitForChild("idle"))
local jab = animator:LoadAnimation(script:WaitForChild("jab"))
local rightcross = animator:LoadAnimation(script:WaitForChild("rightstraight"))
local lefthook = animator:LoadAnimation(script:WaitForChild("lefthook"))

local currentPunch = 0
local debounce = false

local function punch()
    
    if debounce then return end
    
    debounce = true
    if currentPunch == 0 then
        
        jab:Play()
        hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4.5, 1), 10, 0.1)
        task.wait(0.5)
        jab:Stop()
        debounce = false
        
    elseif currentPunch == 1 then
        
        jab:Play()
        hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4.5, 1), 10, 0.1)
        task.wait(0.5)
        jab:Stop()
        debounce = false
        
    elseif currentPunch == 2 then
        
        rightcross:Play()
        hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4.65, 1), 10, 0.1)
        task.wait(0.5)
        rightcross:Stop()
        debounce = false
        
    elseif currentPunch == 3 then
        
        lefthook:Play()
        hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4, 1), 10, 0.2)
        task.wait(0.5)
        lefthook:Stop()
        debounce = false
        
    end
    
    if currentPunch == 3  then
        
        currentPunch = 0
        debounce = true
        wait(1.5)
        debounce = false
        
    else
        
        currentPunch += 1
        
    end
    
end

cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)

and the server sided one:

local rs = game:GetService("ReplicatedStorage")

local events = rs:WaitForChild("Events")
local hitboxEvent = events:WaitForChild("Hitbox")

function newHitbox(character, size, offset, damage, linger)
    
    local hrp = character:FindFirstChild("HumanoidRootPart")
    if hrp == nil then return end
    local weld = Instance.new("WeldConstraint")
    local hitbox = Instance.new("Part")
    
    weld.Part0 = hrp
    weld.Part1 = hitbox
    
    hitbox.CanCollide = false
    hitbox.CanQuery = false
    hitbox.Massless = true
    hitbox.Anchored = true
    
    hitbox.Size = size
    hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offset.X + Vector3.new(0, offset.Y)
    hitbox.Parent = character
    
    hitbox.Touched:Connect(function(hit)
        
        if hit.Parent:FindFirstChild("Humanoid") == nil then return end
        
        for _, v in pairs(hitbox:GetChildren()) do
            
            if v:IsA("ObjectValue") then
                
                if v.Value == hit.Parent then return end
                
            end
            
        end
        
        local hitCounter = Instance.new("ObjectValue", hitbox)
        hitCounter.Value = hit.Parent
        
        hit.Parent.Humanoid:TakeDamage(damage)
        
    end)
    
    task.wait(linger)
    hitbox:Destroy()
    
end

hitboxEvent.OnServerEvent:Connect(function(plr, size, offset, damage, linger)
    
    newHitbox(plr.Character, size, offset, damage, linger)
    
end)

LocalScript is in StarterGUI Server Script is in ServerScriptService I can make it so that player slows down when clicking but I just wanna know the answer so I can grow as a programmer.

0

Browse other questions tagged or ask your own question.