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

Preventing parts from moving outside of brick?

Ok, so I know the title may seem like this is something very basic, but I have reached a problem that I have no idea how to solve; I have a fish that is swimming in a pond, it does this by generating random Vector3 positions and CFrame lerping towards them. However, I want to implement a feature to prevent the fish from just plain swimming through parts and out of the map.

Is there a way in rbx.lua that would let me find the positions of the edges of parts, or does anyone know of an easier method that would let me achieve the same thing?

If anyone want's to see the script, here it is:

while wait() do

local part = script.Parent
marker = Vector3.new(128.015, 2.109, 95.71)

local pos = part.CFrame
local increment = Vector3.new(math.random(-10,10),0,math.random(-10,10))



local newPos = part.CFrame + increment


for i = 0,1,0.01 do
	part.CFrame = pos:lerp(newPos, i)
	part.CFrame = CFrame.new(part.CFrame.p, increment)
	wait(0.01)
end



end

1 answer

I'm not really sure how you would do the edges but for the part to move, here you go.

Add a border, then insert this into each of the bricks in the border.

script.Parent.Touched:connect(function(part)
	local pos = part.CFrame
	local increment = Vector3.new(math.random(-10,10),0,math.random(-10,10))
	local newPos = part.CFrame + increment
	for i, v in pairs(script.Parent:getChildren()) do
		if v:IsA("BasePart") and Name ~= "Baseplate" then
			for i = 0, 1, 0.01 do
				part.CFrame = pos:lerp(newPos, i)
				part.CFrame = CFrame.new(part.CFrame.p, increment)
    			wait(0.01)
			end 
		end
	end
end

Hope this helped!

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