Issues with VectorForces not working in scripts

I’m currently attempting to add custom gravity to the core in the core game I’m making, but whenever I playtest the forces and attachments are being created. The force property, however, is remaining unchanged.

Here’s my script:

...
function AddGravity(part: BasePart)
	local Attachment = Instance.new('Attachment')
	Attachment.Position = Vector3.new(0,0,0)
	Attachment.Parent = part	
	
	local VectorForce = Instance.new('VectorForce')
	VectorForce.Enabled = true
	VectorForce.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
	VectorForce.ApplyAtCenterOfMass = true
	VectorForce.Attachment0 = Attachment
	VectorForce.Parent = part
	
	UpdateGravity(part)
end

function UpdateGravity(part: BasePart)
	local r = Core.CFrame.Position - part.CFrame.Position
	local Force = Gravity * part:GetMass() * Core:GetMass() / r.Magnitude ^ 2
	
	local VectorForce = part:FindFirstChildOfClass('VectorForce')
	VectorForce.Force = Force * r.Unit
end
...
RunService.Heartbeat:Connect(function(dt)
	for _, part in ipairs(CollectionService:GetTagged('GravityAffected')) do
		UpdateGravity(part)
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.HumanoidRootPart:AddTag('GravityAffected')
	end)
end)
...

Any help with this would be much appreciated!

It looks like you never actually call the AddGravity function. Is it called somewhere else?

It’s called whenever a part is added to the tag

Full script to avoid further confusion:

local CollectionService = game:GetService('CollectionService')
local Players = game:GetService('Players')
local RunService = game:GetService('RunService')

local Gravity = 6.67408 * 10^-11
local Core = script.Parent.Main

function AddGravity(part: BasePart)
	local Attachment = Instance.new('Attachment')
	Attachment.Position = Vector3.new(0,0,0)
	Attachment.Parent = part	
	
	local VectorForce = Instance.new('VectorForce')
	VectorForce.Enabled = true
	VectorForce.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
	VectorForce.ApplyAtCenterOfMass = true
	VectorForce.Attachment0 = Attachment
	VectorForce.Parent = part
	
	UpdateGravity(part)
end

function UpdateGravity(part: BasePart)
	local r = Core.CFrame.Position - part.CFrame.Position
	local Force = Gravity * part:GetMass() * Core:GetMass() / r.Magnitude ^ 2
	
	local VectorForce = part:FindFirstChildOfClass('VectorForce')
	VectorForce.Force = Force * r.Unit
end

for _, part in ipairs(CollectionService:GetTagged('GravityAffected')) do
	if not part:IsA('BasePart') then continue end
	
	AddGravity(part)
end

CollectionService:GetInstanceAddedSignal('GravityAffected'):Connect(function(object)
	if not object:IsA('BasePart') then return end
	
	AddGravity(object)
end)

CollectionService:GetInstanceRemovedSignal('GravityAffected'):Connect(function(object)
	if not object:IsA('BasePart') then return end
	
	if object:FindFirstChild('VectorForce') then
		object:FindFirstChild('VectorForce'):Destroy()
	end
end)

RunService.Heartbeat:Connect(function(dt)
	for _, part in ipairs(CollectionService:GetTagged('GravityAffected')) do
		UpdateGravity(part)
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.HumanoidRootPart:AddTag('GravityAffected')
	end)
end)

Fixed, it seems my number for gravity was just way too small

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.