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

Zero gravity bodyforce not working on welded parts. Possible reasons?

I want certain parts on my place to float. My script works perfectly if the affected parts are not welded together. But somehow when they are together they fall to the ground.

	repeat
		b = b+1
		local antigrav = Instance.new("BodyForce")
		antigrav.Force = Vector3.new(0,game.Workspace.Gravity* parts[b]:GetMass(),0)
		antigrav.Parent = parts[b]
		
	until b == #parts

1 answer

First off, don't use a repeat loop for this. You'll want to use a for loop to iterate through any BasePart with mass. Like this...

local GetMass = function(Object)
	local Mass = 0
	Recursive = function(Parent)
		for _,p in next,Parent:GetChildren() do
			if p:IsA("BasePart") then
				Mass = Mass + p:GetMass()
			end
			Recursive(p)
		end
	end
	if Object:IsA("BasePart") then
		Mass = Mass + Object:GetMass()
	end
	return Mass
end

What you're doing is putting a force in every single part of the model, I'm not sure but I believe this is breaking the welds. What you can do is put that force into a single object like this...

local MakeFloat = function(Object)
	local Mass = GetMass(Object)
	local AntiForce = Instance.new("BodyForce")
	AntiForce.Force = Vector3.new(0,workspace.Gravity*Mass,0)
	AntiForce.Parent = Object -- You will need to change this to a part inside of the model. i.e. Object.Part
end

Hope this helps!

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