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

Making two "objects" do something if one condition is met ? just for one though

Asked by
Hizar7 102
5 years ago

What im trying to achieve is a simple script that will allow two objects to be inside the same script and each will do something if a condition is met. So object one is block & object two is rock.

if target == block then
-- game.Workspace.Part.Brickcolor = game.Workspace.Part.Brickcolor.New("Yellow")-- 
else if the target == rock then 
-- game.Workspace.Part.Brickcolor = game.Workspace.Part.Brickcolor.New("Yellow")

so i would like them to both turn the part yellow, but i only want to type out the brickcolor once, not twice.also keep in mind i;m using block & rock but i will in reality be using about 500 differently named objects, please Help!

2 answers

Log in to vote
1
Answered by 5 years ago

A simple solution is to use or/or(take a look at both links. They are not entirely clear though so if you have additional questions comment them below my answer). It takes both conditional statements into consideration and if at least one is true it returns true to the if statement. Example:

if target == block or target == rock then
    --code
end

it should be noted that you can use more than one or:

if true or false or true then
    --code
end

and if at least one of the conditions is true it passes. However, a larger amount of ors are not recommended and you may want to reconsider the logic of your code if that is happening. I hope I helped and have a great day scripting!

Ad
Log in to vote
1
Answered by
ABK2017 406 Moderation Voter
5 years ago

There’s a number of ways to it. Depending on what you want to do, how many parts are in each model, etc., you may just want a generic loop. A simple loop that would change the color of every part in a model named Rock could be something like...

local Rock = game.Workspace.Rock
local Parts = Rock:GetChildren()


for _, Part in pairs(Parts) do
    Children.BrickColor = BrickColor.new("Really red")
end

Again I would do some research on loops...as well as conditions such as IsA.

https://www.robloxdev.com/articles/Roblox-Coding-Basics-Loops#For

As well as conditions, like IsA...from the DevForum...an example of a loop changing a characters limb colors.

local function paintFigure(character, color)
    -- Iterate over the child objects of the character
    for _, child in pairs(character:GetChildren()) do
        -- Filter out non-part objects, such as Shirt, Pants and Humanoid
        -- R15 use MeshPart and R6 use Part, so we use BasePart here to detect both:
        if child:IsA("BasePart") then
            child.BrickColor = color
        end
    end
end
paintFigure(game.Players.Player.Character, BrickColor.new("Bright blue"))

Answer this question