Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Humanoid won't take damage?

Asked by 4 years ago

The part is cloned from replicated storage from a another script after a tool is activated.

This script won't harm anyone: Feel free to ask questions!!!!

local part = game.Workspace:FindFirstChild("Part")
while true do
    wait(0.1)
    if part ~= nil then
        for i, v in pairs(part)do
            v.Touched:Connect(function(hit)
            local enemyHumanoid = hit.Parent:FindFirstChild("Humanoid") 
            local alreadyHit = Instance.new("BoolValue")
            alreadyHit.Parent = hit.Parent
            alreadyHit.Name = "AlreadyHit"
            enemyHumanoid:TakeDamage(50)
            spawn(function()
                wait(1)
                alreadyHit:Destroy()
                end)
            end)
        end
    end
end

Thanks for the Help!

0
If you are wanting to do something like damage per second, i'd recommend using either region 3, or magnitude. The way you have this script, it is going to slow down the game by alot. You are connecting the touched function every time it loops. Despayr 505 — 4y
0
secondly, you don't need the 'for' loop, as I am guessing that your 'Part' doesn't have any children that you need to grab. Despayr 505 — 4y
0
Question 1: Is your 'Part' a model, or a physical part, and does it have any children? Despayr 505 — 4y
0
Um it seems your doing a hitbox type of thing. You should put a invisible brick overlapping your thing and connect the touched from there 123nabilben123 499 — 4y
View all comments (2 more)
0
I think you cannot put a function into a if statement, loop or while waits. It I what you Did at line 6. TheRealPotatoChips 793 — 4y
0
the part is not a model awesomemode14 68 — 4y

1 answer

Log in to vote
0
Answered by
Despayr 505 Moderation Voter
4 years ago

I'd recommend using either region3 or raycasting, but Magnitude works alright.

With what you have done, you are connecting the touched function to the part with every loop. This isn't a good thing.

Secondly, you were using a for pairs loop for a singular part. This won't work because it isn't a table.

local part = game.Workspace:FindFirstChild("Part")
local maxdist = 7 --max distance from the part


local function search()

    local children = workspace:GetDescendants()

    local characters = {}

    local humanoids = {}

----------------------------------------------------------------    
    for _, v in pairs(children) do

        if v:IsA("Model") then --grabs models only

            if v:FindFirstChildOfClass("Humanoid") then
                if v:FindFirstChildOfClass("Humanoid").Health >0 then
                    table.insert(characters, #characters + 1, v)
                end

            end
        end
    end
----------------------------------------------------------------        

    for _, v in pairs(characters) do
        --checking the distance between parts
        local primarypart = v.PrimaryPart

        if primarypart then
            local distance = (primarypart.Position - part.Position).Magnitude
            if distance <= tonumber(maxdist) then
                table.insert(humanoids, #humanoids + 1, v)
            end
        end 
    end
----------------------------------------------------------------    


    for _, humanoid in pairs(humanoids) do
        --damaging the humanoids
        local human = humanoid:FindFirstChildOfClass("Humanoid")
        if human then
            if human.Health >0 then
                human:TakeDamage(50)
            end
        end

    end
----------------------------------------------------------------    
    children = nil
    characters = nil
    humanoids = nil

end



while part ~= nil do
    wait(0.5)
    search()
end

Maybe test this script out. If it isn't to your liking, then I'd suggest looking up both region3, and raycasting.

https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox https://developer.roblox.com/en-us/api-reference/datatype/Region3

Ad

Answer this question