#Is there anything wrong with my code? I randomly die sometimes

1 messages · Page 1 of 1 (latest)

distant prairie
#
local meatEvent = game.ReplicatedStorage.Hitboxes.MeatEvent
local meatBox = game.ReplicatedStorage.Hitboxes.MeatHitbox

local run = game:GetService("RunService")

--meatsword
meatEvent.OnServerEvent:Connect(function(player)
    local char = player.Character
    if not char then return end
local parentModel = char
    local hitbox = meatBox:Clone()
    
    hitbox.Parent = workspace
    local weld = Instance.new("Weld")
    weld.Parent = char:FindFirstChildOfClass("Tool"):WaitForChild("Blade")
    weld.Part0 = char:FindFirstChildOfClass("Tool"):WaitForChild("Blade")
    weld.Part1 = hitbox
    weld.C0 = CFrame.new(0, 0, 0)
    weld.C1 = CFrame.new(0, 0, 0)
run.Heartbeat:Connect(function()
    local parts = workspace:GetPartsInPart(hitbox)
        for _, part in pairs(parts) do
            local parent = part.Parent
            if parent and parent:FindFirstChild("Humanoid") then
                if parent ~= parentModel then
                    parent.Humanoid:TakeDamage(5)
                end
            end
            end
end)

    game.Debris:AddItem(hitbox, 0.7)
    end)
near zinc
#

I believe it has to do with your run.Heartbeat:Connect and that loop

distant prairie
#

How can i fix it?

#

i think thats the problem too

#

Alr made a function

olive creek
# distant prairie ``` local meatEvent = game.ReplicatedStorage.Hitboxes.MeatEvent local meatBox = ...

local meatEvent = game.ReplicatedStorage.Hitboxes.MeatEvent
local meatBox = game.ReplicatedStorage.Hitboxes.MeatHitbox
local Debris = game:GetService("Debris")

meatEvent.OnServerEvent:Connect(function(player)
local char = player.Character
if not char then return end

local tool = char:FindFirstChildOfClass("Tool")
if not tool then return end

local blade = tool:FindFirstChild("Blade")
if not blade then return end

-- Clone hitbox
local hitbox = meatBox:Clone()
hitbox.Parent = workspace
hitbox.CFrame = blade.CFrame
hitbox.CanCollide = false

-- Weld to blade
local weld = Instance.new("WeldConstraint")
weld.Part0 = blade
weld.Part1 = hitbox
weld.Parent = hitbox

-- Detect touching parts
local connection
connection = game:GetService("RunService").Heartbeat:Connect(function()
    if not hitbox.Parent then
        connection:Disconnect()
        return
    end

    for _, part in ipairs(hitbox:GetTouchingParts()) do
        local parent = part.Parent
        if parent and parent:FindFirstChild("Humanoid") and parent ~= char then
            parent.Humanoid:TakeDamage(5)
        end
    end
end)

-- Remove hitbox after 0.7s
Debris:AddItem(hitbox, 0.7)

end)

#

I fixed it for you and made it look better.

distant prairie
#

oH TY

#

wait

#

so gettouchingparts

#

is better than

#

getpartsinpart?>

#

@olive creek

#

ima not use that

#

since its buggy sometimes

#

it doesnt hit sometimes

olive creek
#

How is it buggy I can try to fix it

distant prairie
#

it sometimes doesnt register hit

#

and also can you add a cooldown togetting hit

#

it kills them instantly

#
local char = player.Character
    if not char then return end
local parentModel = char
    local hitbox = meatBox:Clone()
    hitbox.Parent = workspace
    local weld = Instance.new("Weld")
    weld.Parent = char:FindFirstChildOfClass("Tool"):WaitForChild("Blade")
    weld.Part0 = char:FindFirstChildOfClass("Tool"):WaitForChild("Blade")
    weld.Part1 = hitbox
    weld.C0 = CFrame.new(0, 0, 0)
    weld.C1 = CFrame.new(0, 0, 0)
    
setupHitboxDamage(hitbox, char)
game.Debris:AddItem(hitbox, 0.7)
end)


--Function for hitboxes
function setupHitboxDamage(hitbox, parentModel)
    run.Heartbeat:Connect(function()
        local parts = workspace:GetPartsInPart(hitbox)
        for _, part in pairs(parts) do
            local parent = part.Parent
            if parent and parent:FindFirstChild("Humanoid") then
                if parent ~= parentModel then
                    parent.Humanoid:TakeDamage(5)
                end
            end
        end
    end)
end
#

try my script

prime kestrel