How to get a individual value from a for loop?

What is the issue? So, I want to get an individual value from my for loop, this ‘individual value’ would be getting just one rock from my rock module to work with it, and I want to do this with all of them, but I have no idea how to do it.

here is my script:

function module.RockFlip(result)
	local numberOfRocks = 9
	for i = 1, numberOfRocks do
		local rock = Instance.new("Part", workspace.fx)
		rock.Size = Vector3.new(4,6,1)
		rock.CFrame = CFrame.new(result.Position,result.Position + result.Normal)
		rock.Anchored = true
		rock.CanQuery = false
		rock.CanCollide = false
		rock.CanTouch = false
		
		local detect = workspace:Raycast(rock.Position + result.Normal * 4,result.Normal * -10,params)
		if detect then
			rock.Position = detect.Position
			rock.Material = detect.Material
			rock.Color = detect.Instance.Color
			rock.Position = rock.Position + result.Normal * -1
			
			task.spawn(function()
				game.Debris:AddItem(rock,5)
			end)
		end
	end
end

you can use the ‘return’ keyword to return a value

function foo()
return 5
end

local a = foo() -- a will be 5 because foo returns 5

ok, i’ll try and see if it works

1 Like

i think i cant use a function like this one

a local function works in this case?

you can do smt like return rock.Instance

it should work with any function
try this

function module.RockFlip(result)
	local numberOfRocks = 9
	for i = 1, numberOfRocks do
		local rock = Instance.new("Part", workspace.fx)
		rock.Size = Vector3.new(4,6,1)
		rock.CFrame = CFrame.new(result.Position,result.Position + result.Normal)
		rock.Anchored = true
		rock.CanQuery = false
		rock.CanCollide = false
		rock.CanTouch = false

		local detect = workspace:Raycast(rock.Position + result.Normal * 4,result.Normal * -10,params)
		if detect then
			rock.Position = detect.Position
			rock.Material = detect.Material
			rock.Color = detect.Instance.Color
			rock.Position = rock.Position + result.Normal * -1
			task.spawn(function()
				game.Debris:AddItem(rock,5)
			end)
			return rock.Instance -- will return rock Note that a function will close once return is called
		end
	end
end

and outside the module
note that the rock will be nil after 5 seconds because you used game.Debris(rock,5)

local rock = module.RockFlip(result)

if you want to return all the rocks then you will need to use a table and then return it at the end of the loop

function module.RockFlip(result)
	local numberOfRocks = 9
	local rocks = {}
	for i = 1, numberOfRocks do
		local rock = Instance.new("Part", workspace.fx)
		rock.Size = Vector3.new(4,6,1)
		rock.CFrame = CFrame.new(result.Position,result.Position + result.Normal)
		rock.Anchored = true
		rock.CanQuery = false
		rock.CanCollide = false
		rock.CanTouch = false

		local detect = workspace:Raycast(rock.Position + result.Normal * 4,result.Normal * -10,params)
		if detect then
			rock.Position = detect.Position
			rock.Material = detect.Material
			rock.Color = detect.Instance.Color
			rock.Position = rock.Position + result.Normal * -1
			task.spawn(function()
				game.Debris:AddItem(rock,5)
			end)
			rocks[i] = rock
		end
	end
	return rocks
end

Don’t understand the question.


function module.RockFlip(result)
	local numberOfRocks = 9
	local rocks = {}
	--
	--
	
	
			task.spawn(function()
				game.Debris:AddItem(rock, 5)
			end)
		end

		table.insert(rocks, rock) -- Save the rock in the table
	end
	return rocks -- Return all rocks for further use ?
end

i mean, get a individual rock to work with it inside the module, like, add a position or a tween for each one

but you already got a rock and changed its position
{AB035E15-E0BA-430E-9CDE-2EF09913A998}

i think the way is use:

For i = 1, 1 do

end

for each

can you show an example of what you are trying to achieve

and just change the value

for i = 1, 2 do

of course

i want to put a different position and a tween for each rock

does this script work? it gives every rock a differant x position

function module.RockFlip(result)
	local numberOfRocks = 9
	for i = 1, numberOfRocks do
		local rock = Instance.new("Part", workspace.fx)
		rock.Size = Vector3.new(4,6,1)
		rock.CFrame = CFrame.new(result.Position,result.Position + result.Normal)
		rock.Anchored = true
		rock.CanQuery = false
		rock.CanCollide = false
		rock.CanTouch = false

		local detect = workspace:Raycast(rock.Position + result.Normal * 4,result.Normal * -10,params)
		if detect then
			rock.Position = Vector3.new(detect.Position.X + rock.Size.X * i, detect.Z, detect.Z)
			rock.Material = detect.Material
			rock.Color = detect.Instance.Color
			rock.Position = rock.Position + result.Normal * -1

			task.spawn(function()
				game.Debris:AddItem(rock,5)
			end)
		end
	end
end

if you mean to handle every rock out of the 9 rocks manually while giving them values manually u can use if statments

function module.RockFlip(result)
	local numberOfRocks = 9
	for i = 1, numberOfRocks do
		local rock = Instance.new("Part", workspace.fx)
		rock.Size = Vector3.new(4,6,1)
		rock.CFrame = CFrame.new(result.Position,result.Position + result.Normal)
		rock.Anchored = true
		rock.CanQuery = false
		rock.CanCollide = false
		rock.CanTouch = false

		local detect = workspace:Raycast(rock.Position + result.Normal * 4,result.Normal * -10,params)
		if detect then
			if i == 1 then
				 -- rock1 code
			elseif i == 2 then
				-- rock2 code
			elseif i == 3 then
				-- rock3 code
			end
			task.spawn(function()
				game.Debris:AddItem(rock,5)
			end)
		end
	end
end

wait, i’ll try this one, looks interesting

ok, get a individual rock by using this works perfectly, thanks!

– this one:

if i == 1 then
blablabla
1 Like