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
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
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
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