Thunderbolt type thing

how can i make a thunderbolt go in the direction of green and yellow but cant go past the blue quadrant


(through a script, has to be randomly generated each play)

I’m not sure how you want to actually create the thunderbolt effect, but to give you some choices you can use the module that @d1tr linked (I’ve used it before, and its pretty easy to use), or you can create your own effect using a method detailed in this thread (though code is not provided).

As to actually do the math for where each bolt with start and stop, it’s not too difficult. Since everything you’re doing is random, its best to keep the context of color out of your code so it works no matter which color is chosen. Since you only want to the get the adjacent colors (ie. green/yellow when blue is selected), you can grab those by finding which color is furthest away (which in this case would be red). You want to ignore the furthest away and the source.

Then, for each lightning bolt, create a bolt from the source to each of the adjacent colors. I’m assuming the small parts on each color is the reference point for each color, and is in the exact center. If so, you can do some pretty simple math, getting the direction between each part and making a bolt half the size of the square, so it reaches just the edge.

-- direction vector to get from source to adjacent part. Divide by 2 to get half the length (which should border edge of square)
local direction = (adjecentPart.Position - source.Position)/2
-- add direction vector to source position to get where the lightning should end
local endPoint = source.Position + direction

Then you can create whatever lightning affect you want between the source position and the endPoint position we just found.