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

Is there a better way to make this block of code neat / A different way to script?

Asked by 4 years ago
Edited 4 years ago
local SteamedMilkMachine = game.Workspace:WaitForChild('SteamedMilkMachine')

local EspressoMachine = game.Workspace:WaitForChild('EspressoMachine')

SteamedMilkMachine.Touched:connect(function(hit)
    if hit.Parent.Name == 'Cup' then
       hit.Parent.Name = 'Steamed Milk'
    end
end)

EspressoMachine.Touched:connect(function(hit)
    if hit.Parent.Name == 'Cup' then
               hit.Parent.Name = 'Espresso''
    end
end)

2 answers

Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
4 years ago

It's a bit confusing to what your doing. But, This looks a bit better:

local count = 0
script.Parent.Touched:Connect(function(hit)
    local hp = h.Parent
        count = count + 1 -- Depends What Is Touching It.
    if h.Parent.Name == 'Cup' then
        hp.Name = "["..count.."] Steamed-Milk"
    elseif h.Parent.Name == 'Steamed-Milk' then
        hp.Name = "["..count.."] Cappuccino"
    else
        hp.Name = "["..count.."] Espresso Blend"
    end
end)
0
I was going to change the parents which were going to be touched after I got the answer... is there another way? ISkyLordDoge 37 — 4y
0
Like i said, your being really confusing. Can you elaborate more? Farsalis 369 — 4y
0
Sure, just give me time to edit it! Thank you! :) ISkyLordDoge 37 — 4y
0
There you go! I hope it's easier for you to read now. I've fixed some mistakes and gave an extra help to understand what I'm saying by changing the parents! Thank you :) ISkyLordDoge 37 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I don't think efficiency and neatness are really your problem here, the code just has bugs.

script.Parent.Touched:connect(function(h)--Change 'script.Parent'
    if h.Parent.Name == 'Cup' then-- This station is Steamed Milk.
       h.Parent.Name = '[1]Steamed-Milk'
    elseif h.Parent.Name == 'Cup' then
        h.Parent.Name = '[2]Steamed Milk'
    end
end)
script.Parent.Touched:connect(function(h)--Change 'script.Parent'
    if h.Parent.Name == 'Steamed-Milk' then-- This station is an Espresso Blend.
       h.Parent.Name = '[3]Cappuccino'
    elseif h.Parent.Name == 'Cup' then
       h.Parent.Name = '[4]Espresso Blend'
    end
end)

The first bug is that your first if-elseif block has both ifs checking the same condition, h.Parent.Name == 'Cup', so you'll never end up with a cup named '[2]Steamed Milk' because that's unreachable code.

The next bug is that the other machine looks for a cup named 'Steamed-Milk' with an exact string compare (not accounting for the [1] or [2]), and you never name a cup this. string.find(h.Parent.Name,"Steamed-Milk") would work... but ...only if you don't use 'Steamed-Milk' in one place and 'Steamed Milk' in the other. Hypens matter (bug 3).

The fourth bug is that you are making a cappuccino by adding espresso to steamed milk, but this is not how you make a cappuccino. You add steamed milk to espresso, the order matters, so you have the wrong machine producing this drink.

0
Really happy you found out my bugs! Thank you! :) ISkyLordDoge 37 — 4y

Answer this question