Scripters a simple question!

what is better?:

if hit.Parent:FindFirstChildOfClass("Humanoid") then
hit.Parent.Humanoid:TakeDamage(10)
elseif hit.Parent.Parent:FindFirstChildOfClass("Humanoid") then
hit.Parent.Parent.Humanoid:TakeDamage(10)
end


or

if hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChildOfClass("Humanoid") then
hit.Parent.Humanoid:TakeDamage(10) or hit.Parent.Parent.Humanoid:TakeDamage(10)
end

if some part of the spelling is wrong it doesn’t matter it’s hard to write a script in DevForum. also in the TakeDamage part it is necessary to put instead of:hit.Parent.Parent.Humanoid:TakeDamage(10) or hit.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(10)

Storing the Humanoid under a variable would be better and it would make your code look neater. I would also like to mention that if you want to check if its a player touching the part you can use :GetPlayerFromCharacter().

For example:

part.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")    

    if Player and Humanoid then
        Humanoid:TakeDamage(10)
    end
end)
2 Likes

the order is the least important thing and besides I’m not talking about a touched event I’m talking about lightning and multiple systems that I personally do not use touched almost also if the lightning strikes an accessory what do I do? well hit.Parent.Parent.Humanoid but answering the question I put is what I need

Best Version is:

if hit.Parent then

if hit.Parent:FindFirstChild('Humanoid') then

hit.Parent.Humanoid:TakeDamage(10)

end

if hit.Parent.Parent and hit.Parent.Parent:FindFirstChild('Humanoid') then

hit.Parent.Parent.Humanoid:TakeDamage(10)

end

end

Is this what you are looking for?

This would simply work:

if Hit.Parent:FindFirstChildWhichIsA("Humanoid") then
    ...
end

sure sure 100# since that’s how I’m doing it and I have the longest code left. Although I need you to help me rather with the optimization because I want the server to go through the function faster

To take his life I would:
Hit.Parent:FindFirstChildWhichIsA(“Humanoid”):TakeDamage(5)
remember I use lightning bolts and when you hit an accessory it’s a very different case

Use collectionService then that will be the most optimized way to check for characters i guess

but would it work for lightning?

type or paste code here

CollectionService is tag system, so istead checking for humanoid you can check it hit.Parent is tagged with etc ‘character’

if CollectionService:HasTag(hit.parent, "character") then

I hope you’re right because I’m working with ray cast

oh I understand, collectionservice is a more optimized function of Find something Now, it would also work for dummies or zombies, in case of taking life, what would it do?

Using CollectionService to tag and recognize characters, including NPCs like zombies or dummies, can be quite effective. After tagging the humanoid characters, you can simply check if the entity that your raycast hit has the ‘character’ tag.

Here is an example of how you might use this to damage a character:

local CollectionService = game:GetService("CollectionService")

part.Touched:Connect(function(hit)
    -- Check if the part that was hit belongs to an entity tagged as 'character'
    if CollectionService:HasTag(hit.Parent, "character") then
        local humanoid = hit.Parent:FindFirstChild("Humanoid")

        -- Make sure the humanoid exists before attempting to apply damage
        if humanoid then
            humanoid:TakeDamage(10)
        end
    end
end)

In this script, when the part is touched, it checks if the parent of the part that was hit has the ‘character’ tag. If it does, it attempts to find a Humanoid in the same parent. If a Humanoid is found, it applies 10 damage to it.

You can use this similar logic with your raycast system. If your raycast hit detects an entity, check if it has the ‘character’ tag, then apply damage to the humanoid if it exists.

However, remember that for this to work, you need to have all your characters (players, zombies, dummies etc.) tagged as ‘character’ in the CollectionService.

Turn of collisions for accessories

Thanks for your help but excuse me how do I tag them?

You can use a plugin to do it easy that what i use (9) Tag Editor - Roblox

Literally just open the plugin gui create a new tag, select the object and tick the box for that tab in the plugin gui and the object is tagged, also u can tag object via script for example on CharacterAdded Event

1 Like

I’m not sure how to tag without the plugin and without the script but if you wanna tag an object via script here is how to do it:

local CollectionService = game:GetService("CollectionService")

-- Assuming 'character' is the instance you want to tag
CollectionService:AddTag(character, "character")