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

Script stopping, no errors?

I wrote a code down below that's supposed to counteract gravity in parts that the "manipulator" part is pointing at. However, sometimes the loop in the function "manipulate" runs once, twice, or not at all. I know that it's not the if statement on line 10 that's stopping the code, because I tried to troubleshoot my code there by printing a phrase, and it didn't print anything. Any help?

local event = Instance.new("RemoteEvent",game.ReplicatedFirst)
event.Name = "descendantadded"

local parts = {}

local function manipulate(manipulator,part)
	coroutine.resume(coroutine.create(function()
		local n
		while true do
			if not manipulator or not part then
				return
			end
			local p = manipulator:pointToObjectSpace(part.Position)
			if p.x <= -manipulator.Size.x/2 and
			p.x >= -manipulator.Size.x/2 and
			p.z <= -manipulator.Size.z/2 and
			p.z >= -manipulator.Size.z/2 and
			p.y >= manipulator.Size.y/2 then
				if not part:FindFirstChild("NoGravity") then
					n = Instance.new("BodyForce",part)
					n.Name = "NoGravity"
				end
				n.Force = Vector3.new(0,196.2,0)*part:GetMass()
			else
				if part:FindFirstChild("NoGravity") then
					n:Destroy()
				end
			end
			game:GetService("RunService").RenderStepped:wait()
		end
	end))
end


local function findparts(manipulator)
	event.OnServerEvent:connect(function(nothing,descendant)
		manipulate(manipulator,descendant)
	end)
	for i,v in pairs(parts) do
		manipulate(manipulator,v)
	end
end

workspace.DescendantAdded:connect(function(descendant)
	if descendant:IsA("Part") and descendant.Name ~= "Baseplate" then
		if descendant.Name == "Gravity Manipulator" then
			findparts(descendant)
		else
			table.insert(parts,#parts+1,descendant)
			event:FireServer(descendant)
		end
	end
end)

workspace.DescendantRemoving:connect(function(descendant)
	for i,v in pairs(parts) do
		if v == descendant then
			table.remove(parts,i)
			break
		end
	end
end)

local function indexparts(instance)
	local children = instance:GetChildren()
	if #children > 0 then
		for i,v in pairs(children) do
			if #v:GetChildren() > 0 then
				indexparts(v)
			end
			if v:IsA("Part") and v.Name ~= "Baseplate" then
				if v.Name == "Gravity Manipulator" then
					findparts(v)
				else
					table.insert(parts,#parts+1,v)
				end
			end
		end
	end
end

indexparts(workspace)

1 answer

Nevermind, I fixed it. Line 13 was casting the method on the part "manipulator", rather than its cframe.

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