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

how to make this movement system script cleaner?

on line 2, is there a better way to get the parent's move stat? i dont want to have to type a function for each number (4,5,6,7,8, etc.)

also is there a way to project selection boxes from line 4 according to the move stat ?

like a move stat of 3 would be 3 bricks in each direction, 4 would be 4 in each direction, etc.

(if it's math, i suck at math)

for i, v in pairs(game.Workspace.Map:GetChildren()) do
	if script.Parent.Move.Value == 3 then
		if v.Name == "TerrainB" then
			Instance.new("SelectionBox", v)
		
	if script.Parent.Move.Value == 4 then
		if v.Name == "TerrainB" then
			Instance.new("SelectionBox", v)
	
	if script.Parent.Move.Value == 5 then
		if v.Name == "TerrainB" then
			Instance.new("SelectionBox", v)
			
	end
		end
			
	end
		end
			end

	end
		end

1 comment

0

Tab your code correctly.

2 answers

✓ Accepted answer

This may be a small improvement for superalp1111 answer:-

You might be able to use if we do not need to check the value ranges each time:-

if script.Parent.Move.Value > = 3 and script.Parent.Move.Value <= 10 then
	for i, v in pairs(game.Workspace.Map:GetChildren()) do 
		if v.Name == "TerrainB" then 
     		Instance.new("SelectionBox", v) 
		end
	end
end

Method 2, this would be faster as only one check is made instead of two possible checks when putting the "if script.Parent.Move.Value > = 3 and script.Parent.Move.Value <= 10 then " 1st:-

for i, v in pairs(game.Workspace.Map:GetChildren()) do 
	if v.Name == "TerrainB" then 
		if script.Parent.Move.Value > = 3 and script.Parent.Move.Value <= 10 then
			Instance.new("SelectionBox", v) 
		end
	end
end

Hope this helps.

5 comments

0

it made the selection boxes, but it's not visible

0

which method are you using?

0

second one

0

The second method is the same logic as superalp1111 but swapped around, there is no real difference except the checking order

0

oh hmm. i dont know why it isnt visible. it's visible when with another script when i move the mouse over the bricks

If you are going to type absolutely same thing in every if condition, this may help you.

for i, v in pairs(game.Workspace.Map:GetChildren()) do 
	if script.Parent.Move.Value > = 3 and script.Parent.Move.Value <= 10 then		
		if v.Name == "TerrainB" then 
		Instance.new("SelectionBox", v) 
	end
end

So you can type as much conditions as you want if you put "and" and "or" between conditions. For ex. This script will work when value is between 3 and 10, or when equal to 3 or 10.

1 comment

0

thanks. thats one part

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