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

Is this the best way to index something revolving all model parts?

Alright so this is ran through two GUI buttons and a RemoteEvent. It works perfectly fine. My main issue is I feel like there's an easier way to do this.

What the script is suppose to do.

The script checks to see if SteelCage is a model in workspace and if it is then it continues the code. if the function Open is ran, it turns all of door1's parts transparency to 1 and turns off CanCollide then turns all of door2's parts transparency to 0 and turns on CanCollide. elseif the function is Closed, it turns the door1 parts transparency to 0 and turns on CanCollide then turns all of door2s parts transparency to 1 and CanCollide off. | Yes, the variables named door1 and door2 have to be inside the code script sadly so it doesn't break the entire script as I move this item from ServerStorage to Workspace.

My Question.

Is there a way to make this easier or look more clean? Maybe even move it down a button?

--Made By MillerrIAm
------------------Variables----------------
Event = game.ReplicatedStorage.ExtraEvents.CageDoorEvent
------------------Main Script------------------
Event.OnServerEvent:Connect(function(Player,Door)
	if game.Workspace:FindFirstChild("SteelCage") then
		if Door == "Open" then
			local door1 = game.Workspace.SteelCage.Doors.CageDoor1:GetChildren()
			local door2 = game.Workspace.SteelCage.Doors.CageDoor2:GetChildren()
			for i, x in pairs(door1) do
				x.Transparency = 1
				x.CanCollide = false
			end
			for i, v in pairs(door2) do
				v.Transparency = 0
				v.CanCollide = true
			end
		elseif Door == "Close" then
			local door1 = game.Workspace.SteelCage.Doors.CageDoor1:GetChildren()
			local door2 = game.Workspace.SteelCage.Doors.CageDoor2:GetChildren()
			for i, x in pairs(door1) do
				x.Transparency = 0
				x.CanCollide = true
			end
			for i, v in pairs(door2) do
				v.Transparency = 1
				v.CanCollide = false
			end
		end
	else
	end
end)
--[[if Door == "Open" then
		Get all of the children of CageDoor1 & CageDoor2
		Turn all of Door1's parts Transparency to 1 and turn CanCollide off
		then turn all of Door2's parts Transparency to 0 and cancollide on.
	elseif Door == "Close" then
		Get all of the children of CageDoor1 & CageDoor2
		Turn all of Door2's parts Transparency to 1 and turn CanCollide off
		then turn all of Door1's parts Transparency to 0 and cancollide on.]]

3 comments

0

I know the script is a mess, I apologize. I just want to know exactly what I can do. If I can do multiple values for the in pairs (values) then please inform me on a way I can learn how to do this please.

0

I've been looking and attempting for a while and nothings working.

0

FYI again, This script DOES work so I know it's possible.

1 answer

✓ Accepted answer

I gave you an upvote for doing a great job at being descriptive with your question, you don't see that a lot.

Overall your code looks pretty good. If I were you I would use the :IsA method to make sure the current index is a BasePart and you don't accidentally try to change the transparency of something else, causing your code to break.

Additionally, instead of defining Door1 and Door2, you can iterate through the Doors directory in-case you wanted to add more doors in the future.

2 comments

0

Thank you for the upvote! My goal is to have everything in the model, part or not to change transparency and cancollide, hence why I didn't use the :IsA method. Thank you though! How would I iterate through the doors?...

1

for _,v in pairs(doors:GetChildren()) do ... end

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