Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

4 comments

0

I was going to change the parents which were going to be touched after I got the answer... is there another way?

0

Like i said, your being really confusing. Can you elaborate more?

0

Sure, just give me time to edit it! Thank you! :)

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 :)

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.

1 comment

0

Really happy you found out my bugs! Thank you! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.