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

How to use if statement to determine whether a block does damage or not?

Asked by 4 years ago

So I'm trying to figure out how to use an if statement on my part, for example I'm trying to use an if statement so if the part is a specific color it does no damage for example, if the part is orange, it does no damage.

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

A part can only do damage if it is programmed to do so, if you want to check whether it can, use a conditional to check for the Script's name, or if it is active.

local Part = workspace.Part

if (Part.ScriptName) then
   if (part.ScriptName.Active) then
        print(Part.Name.." does damage")
   return end
   print(part.Name.." doesn't damage")
end
0
If ScriptName is not a member of Part, it will error. You need to use FindFirstChild. You randomly used Part and part interchangeably even though part is not a variable. And, Active is not a property of Script, you meant to use Disabled. Overscores 381 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Explanation

When you write if-then statements, keep in mind that organizationally, you also have access to elseif which defines a separate boolean check from your first case (defined by the first if-then) as well as else for any other cases not explicitly defined

In my below example, I'm assuming that "orange" would refer to the specific BrickColor "Deep orange", you can also check the Color Property of a part and match it against a Color3.fromRBG(#, #, #) if you wanted a specific value or range.

Below, I use a Touched event to get the part that touched your part and check that it exists, as well as has a parent that is also a character instance (this is done so that only Player Humanoids are damaged)

TakeDamage is a function of the Humanoid class that you can use to reduce the player's Health

Server Script Example

local part = script.Parent

part.Touched:Connect(function(hit)
    if hit and hit.Parent and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
        local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
        if humanoid then
            if part.BrickColor == BrickColor.new("Deep orange") then
                -- Nothing
            else
                humanoid:TakeDamage(100)
            end
        end
    end
end)

The above code would be placed in a Server Script directly parented under the part you want to damage / not damage players

Answer this question