Local script works correctly but gives me an error and has a stack overflow

  1. What do I want to achieve?

I am trying to create a script that is a workaround of an issue caused by a complicated Gravity Controller which allows the player to walk on any surface with a specific name. I did succeed to do it by making a system that inserts a VectorForce into a HumanoidRootPart of a separate character made by the Gravity controller. It works, and now when I touch a part called PushPart, I am pulled downward.

  1. What is the issue?

The issue is, well the system works but it gives me an error and causes a stack overflow when the player breaks contact with the PushPart, it might create some issues later on.

Why do I have to do it? The Gravity Controller seemingly prevents the visible character we play as from being affected by any kind of forces and impacting the one far away, which controls gravity related things. This results in the gravity fields not affecting the character, they normally affect the player in a world without the gravity controller.

This is the script:

-- Function to display messages
local function showMessage(message)
	print(message)
end

-- Function to insert a VectorForce with an attachment into the HumanoidRootPart in PhysicsWorld
local function insertVectorForce()
	local character = game.Players.LocalPlayer.Character
	if not character then
		character = game.Players.LocalPlayer.CharacterAdded:Wait()
	end

	local playerName = game.Players.LocalPlayer.Name

	local function onTouched(part)
		if part and part:IsA("BasePart") and part.Name == "PushPart" then
			showMessage("Contact with PushPart.")

			-- Find or create the group folder named after the player in PhysicsWorld, idk why I would create it but okay
			local physicsWorldFolder = game.Workspace.PhysicsWorld:FindFirstChild(playerName)
			if not physicsWorldFolder then
				physicsWorldFolder = Instance.new("Model")
				physicsWorldFolder.Name = playerName
				physicsWorldFolder.Parent = game.Workspace.PhysicsWorld
			end

			-- Find or create the VectorForce in the HumanoidRootPart
			local humanoidRootPart = physicsWorldFolder:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				local vectorForce = humanoidRootPart:FindFirstChild("VectorForce")
				local attachment = humanoidRootPart:FindFirstChild("VectorForceAttachment")

				if not vectorForce then
					vectorForce = Instance.new("VectorForce")
					vectorForce.Name = "VectorForce"
					vectorForce.Force = Vector3.new(0, -1000, 0)
					vectorForce.Parent = humanoidRootPart

					if not attachment then
						attachment = Instance.new("Attachment")
						attachment.Name = "VectorForceAttachment"
						attachment.Parent = humanoidRootPart
					end

					vectorForce.Attachment0 = attachment
					vectorForce.Enabled = true
				end
			end
		end
	end

	local function onTouchEnded(part)
		if part and part:IsA("BasePart") and part.Name == "PushPart" then
			showMessage("Contact with PushPart broken.")

			-- Find the group folder named after the player in PhysicsWorld or something
			local physicsWorldFolder = game.Workspace.PhysicsWorld:FindFirstChild(playerName)
			if physicsWorldFolder then
				-- Find the VectorForce in the HumanoidRootPart
				local humanoidRootPart = physicsWorldFolder:FindFirstChild("HumanoidRootPart")
				if humanoidRootPart then
					local vectorForce = humanoidRootPart:FindFirstChild("VectorForce")
					local attachment = humanoidRootPart:FindFirstChild("VectorForceAttachment")

					if vectorForce and attachment then
						-- Remove the VectorForce and its attachment
						vectorForce:Destroy()
						attachment:Destroy()
					end
				end

				-- Remove the group folder if it's empty
				if physicsWorldFolder:GetChildrenCount() == 0 then
					physicsWorldFolder:Destroy()
				end
			end
		end
	end

	character:WaitForChild("HumanoidRootPart").Touched:Connect(onTouched)
	character:WaitForChild("HumanoidRootPart").TouchEnded:Connect(onTouchEnded)
end

-- Start checking for contact and inserting VectorForce
insertVectorForce()

And this is the stuff that pops up on the developer console when I break contact with the PushPart:

If what I provided isn’t enough, here is the file of the game, hopefully it will be helpful:

Gravity system WIP.rbxl (281.0 KB)

I appreciate any help in creating my dream game!

GetChildrenCount() isn’t a real function.

To get the number of children of the model, instead do this:

#physicsWorldFolder:GetChildren()

# basically just returns the number of items in an array.

1 Like

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