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

How do I stop these blocks from rising infinitely?

I'm currently trying to make a block which, depending on the proximity of blocks below, will adjust it's own Y position. At the moment, I'm making the part in which it will raise it's Y position if it's too close to the block below.

The system I'm using is a sensor block (known as 'frf' in the code) which sits not far below the main block (known as 'fr' in the code). The code is supposed to detect whether the sensor block is colliding with anything and, if it is, adjust the height of both blocks by 0.1 then continue checking forever and adjusting the height where necessary.

Unfortunately, when I run the code, both blocks just continue rising infinitely and I cannot see how this would happen. Thoughts?

Here is my code:

local fr = workspace.Drone["Front Right"]
local frf = workspace.Drone["Front Right Floor"]
local arlen = 0
local ar = {frf:GetTouchingParts()}
local ar = 0

while true do
	ar = {frf:GetTouchingParts()}
	arlen = 0
	for key, values in pairs(ar) do
		arlen = arlen + 1
	end
	while arlen > 0 do
		fr.Position = fr.Position + Vector3.new(0,0.1,0)
		frf.Position = frf.Position + Vector3.new(0,0.1,0)
		arlen = 0
		wait(0.05)
	end
end

Thanks in advance for any help!

1 comment

0

You should use better names for your variables so when you are typing code and checking it you can easily tell what a variable means.

1 answer

✓ Accepted answer

Essentially, the GetTouchingParts function already returns a table. You are packing a table into a table, and counting the number of indexes in that table - 'arlen' will always be 1, do you understand?

By the way - the # operator will return the number of indexes in the proceeding table.

local fr = workspace.Drone["Front Right"]
local frf = workspace.Drone["Front Right Floor"]

while wait(1) do
	while #frf:GetTouchingParts() > 0 and wait(.05) do
		fr.Position = fr.Position + Vector3.new(0,0.1,0)
		frf.Position = frf.Position + Vector3.new(0,0.1,0)
	end
end

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