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

Would this script work?

If not, can you tell me how I can fix it? I want it so that you can put it in the Workspace and anypart named Lava or Acid will change reflectance and transparency with respect to the script.

local p = "Lava, Acid"
p.Transparency = 0.5
p.Reflectance = 0.5
wait(0.5)
p.Transparency = 0.5
p.Reflectance = 0.2

2 comments

0

when you say local p = "Lava, Acid" All your doing is defining p as a string that is "Lava,Acid". If you want to change everything in the workspace, you need to use a for loop.

0

Ah, okay.

3 answers

Try using for loops!

for i, v in pairs(Workspace:GetChildren()) do
	if v:IsA"BasePart" and v.Name == "Lava" or v.Name == "Acid" then
		v.Transparency = 0.5
		v.Reflectance = 0.5
	end
end

wait(0.5)

for i, v in pairs(Workspace:GetChildren()) do
	if v:IsA"BasePart" and v.Name == "Lava" or v.Name == "Acid" then
		v.Transparency = 0.5
		v.Reflectance = 0.2
	end
end

for loop can be used to search through any object for it's children, making it excellent at searching objects with same name.

This will make script search through all the Workspace for a part named either "Lava" or "Acid". When it finds part/parts, it/they will change respectively to your likings.

There are two for loops because the first one makes Reflectance of those part 0.5, and the second on turn the Reflectance to 0.2. If there was one loop and wait(0.5) was in the middle (see below), that would make it so it changes parts one by one, rather than every at once.

for i, v in pairs(Workspace:GetChildren()) do
	if v:IsA"BasePart" and v.Name == "Lava" or v.Name == "Acid" then
		v.Transparency = 0.5
		v.Reflectance = 0.5
		wait(0.5)
		v.Transparency = 0.5
		v.Reflectance = 0.2
	end
end

Note: v:IsA"BasePart" makes sure that it's a part, not script so you don't end up changing Reflectance of a script named Acid or Lava.

3 comments

1

You described having two separate for loops, but there is only one in your code example - it will go through each piece one by one just like every other answer so far.

0

Oh right, silly me. I forgot to add the other loop!

0

Edited. Thanks agin for pointing it out!

A more efficient way that you could do this, assuming that the number of Lava and Acid Parts stays the same, is to first store them all in a table, like-so:

dangerousParts = {}

--Store the dangerous parts, lava, acid, electric, poison, you name it...
for _,v in pairs(workspace:GetChildren()) do
	if v.Name == "Lava" or v.Name == "Acid" then
		table.insert(dangerousParts,v)
	end
end

--This runs the 'animation' of the dangerousBricks in a loop, but you can change to run it however you like
--Also notice that in the example you gave Transparency stayed the same the whole time, so no need to add that in too
function SetReflectance(reflectance)
	for _,part in pairs(dangerousParts) do
		if part:IsA("BasePart") then
			part.Reflectance = reflectance
		end
	end
end

while wait(0.5) do
	SetReflectance(0.5)
	wait(0.5)
	SetReflectance(0.2)
end

This answer avoids the problem in all the other answers (that have been posted up until I started writing) that it will adjust the transparency of each "dangerous part" one-by-one with a wait() delay.

EDIT: Tiranin has a good answer too, now.

Try this instead

function AcidLava(part)
part.Transparency = 0.5
part.Reflectance = 0.5
wait(0.5)
part.Reflectance = 0.2
end

for i, v in pairs(game.Workspace:GetChildren()) do
if v.Name == "Lava" or v.Name == "Acid" then
AcidLava(v)
end
end

1 comment

0

This will also pause (yield) for each part, adjusting them slowly one by one instead of simultaneously.

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