Magnet not working

Basically I have tons of parts, and I want them all to conform together to make something that looks equivalent to one part. I have this code inside every part:

local charge = object.Charge
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")

RS.Heartbeat:Connect(function()
	local region = Region3.new(object.Position-Vector3.new(15,15,15),object.Position+Vector3.new(15,15,15))
	local parts = workspace:FindPartsInRegion3(region)
	for i,v in pairs(parts) do
		if v.Name == "Proton" then
			if v.Anchored == false then
				local tweentime = ((v.Size.X*v.Size.Y*v.Size.Z)*(v.Position-object.Position).Magnitude)/100
				local tween = TS:Create(v,TweenInfo.new(tweentime),{Position=object.Position})
				tween:Play()
			end
		end
	end
end)

I also have this code in every part:

local bodyForce = Instance.new("BodyForce")
bodyForce.Force = Vector3.new(0, parent:GetMass() * workspace.Gravity, 0)
bodyForce.Parent = parent

Yet the parts don’t even move.

maybe the Body forces P value is too low? i have no experiences with body force but with body velocities the P determines how much force is exerted on a part

https://developer.roblox.com/en-us/api-reference/class/BodyForce

unless im totally wrong, haha :sweat_smile:

Well the thing is that script just removes gravity from only the parts, since protons/neutrons are nearly weightless

And the script works fine, it is just the magnet script that doesn’t work.

1 Like

are there any errors in the magnet script?

Nothing that the game tells me

You could try using AlignPosition and AlignOrientation. They are two of the replacements for bodymovers because they are considered legacy now.

For which script?

charsss

To connect the parts/magnets together more realistically you can use the new versions of the bodymovers.

I don’t care how they come together, I just care that they do come together, but they don’t.

Also I can’t really do much changing since I have that script in every part.

You can debug the code with print statements:

local charge = object.Charge
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")

RS.Heartbeat:Connect(function()
	local region = Region3.new(object.Position-Vector3.new(15,15,15),object.Position+Vector3.new(15,15,15))
	local parts = workspace:FindPartsInRegion3(region)
	for i,v in pairs(parts) do
		if v.Name == "Proton" then
			if v.Anchored == false then
				local tweentime = ((v.Size.X*v.Size.Y*v.Size.Z)*(v.Position-object.Position).Magnitude)/100
				print("Code found proton, tweenTime is "..tweentime)
				local tween = TS:Create(v,TweenInfo.new(tweentime),{Position=object.Position})
				tween:Play()
			end
		end
	end
end

If the print statement doesn’t run then there aren’t parts named Proton that are in the region and unanchored.

If the tween time is huge maybe the tween is just running very slowly.

It is finding it, it just doesn’t seem to find it until it starts moving.

Why not loop through all workspace descendants and check magnitude?

Because what would that do?

char

Magnitude to check if the part is close to the magnet,.

In the script that creates the magnet, it already does that with a Region3.

1 Like