CollectionService Script doesn't work

Hello!

Once again I’m asking for some assistance here.
Been getting into some classic ROBLOX projects, and want to make it slightly more modern!

I looked into TagEditor and CollectionService, and managed to get TagEditor to run, since it’s not exactly rocket science.

The script is supposed to make the trees move just slightly, make them look as if wind is impacting their physics or such, here it is:

Yes, the script is inside of ServerScriptService

local CollectionService = game:GetService("CollectionService")

for _part in CollectionService:GetTagged("TreeTop")do
	pos = script.Parent.Position
	pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
	x = 0
	z = 0
	T = -99999
	tall = script.Parent.Size.Y / 2
	math.randomseed(tick())
	rand = (math.random(0,20))/10
	while true do
		x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
		z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
		script.Parent.CFrame =
			CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
		wait()
		T = T + 0.12
	end
end

Only issue really is that the trees don’t move, and output shows "Position is not a valid member of ServerScriptService.
I am total garbage at scripting, hence the request for help.

Thanks for any tips/solutions!

Can you provide a screenshot of the current layout in your Tag Editor window?

1 Like


I basically just selected the “TreeTop” Part that I wanted to animate, and tagged it.

Not sure if this is what you are looking for?
I watched a tutorial about CollectionService, maybe they explained it wrong.

for _part in CollectionService:GetTagged("TreeTop")do

Looks like you missed a comma inbetween _ and part.
Let me know if fixing this solves the problem.

Any reason not to use it this way? I am not the best at using CollectionService, however, so you could take whatever I say with a grain of salt.

for _,part in pairs CollectionService:GetTagged("TreeTop") do

I don’t know if this works or not…

1 Like

Unfortunately not, it still says “Position is not a valid member of ServerScriptService”

What line does that error occur at?

As the error says, Position is not a property of ServerServiceStorage, Position must be for the object with the tag, right?

local CollectionService = game:GetService("CollectionService")
for _, part in pairs(CollectionService:GetTagged("TreeTop")) do
	local pos = part.Position
	pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
	
	local x, z, T = 0, 0, -99999
	local tall = part.Size.Y / 2
	math.randomseed(tick())
	
	local rand = (math.random(0,20))/10
	task.spawn(function()
		while true do
			x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
			z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
			part.CFrame = CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
			task.wait()
			T += 0.12
		end
	end)
end
1 Like

Good catch, didn’t notice that they originally had a wait() in there.

That wasn’t really related to the bug, just that the author misused the variable.

Yes, because I am tampering with the position of the TreeTop to make it move around (slightly of course, to give the idea of wind existing)

It doesn’t display which line it occurs at, but it puts out this:
19:56:25.651 Position is not a valid member of ServerScriptService “ServerScriptService” - Server - Script:4

If my answer solved your problem, mark it as a solution, if not, is there another bug?

1 Like

Change

pos = script.Parent.Position

to

pos = part.Position

The reason this error would occur is because the script is inside of ServerScriptService.

To add on to this,

local CollectionService = game:GetService("CollectionService")

for _,part in CollectionService:GetTagged("TreeTop") do
	pos = part.Position
	pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
	x = 0
	z = 0
	T = -99999
	tall = script.Parent.Size.Y / 2
	math.randomseed(tick())
	rand = (math.random(0,20))/10
	task.spawn(function()
		while true do
			x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
			z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
			script.Parent.CFrame =
				CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
			task.wait()
			T = T + 0.12
		end
	end)
end
1 Like

Okay, we are one step further:

I did what you said. It throws up an error that “Size” is not a valid member of ServerScriptService. I changed it from:

tall = script.Parent.Size.Y / 2

to:

tall = script.Part.Size.Y / 2

Yet it now spits out that “Part is not a valid member of etc.”

local CollectionService = game:GetService("CollectionService")

for _,part in CollectionService:GetTagged("TreeTop") do
	pos = part.Position
	pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
	x = 0
	z = 0
	T = -99999
	tall = part.Size.Y / 2
	math.randomseed(tick())
	rand = (math.random(0,20))/10
	task.spawn(function()
		while true do
			x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
			z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
			part.CFrame =
				CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
			task.wait()
			T = T + 0.12
		end
	end)
end

Try that.

Yup, works, only that it’s ridiculously fast, and it removed the other TreeParts I tagged? They just aren’t there anymore

task.wait(0.06)

Try fiddling with the number. As for the latter issue, I have no clue what’s wrong.

1 Like

Is it maybe because the TreeTops are inside of a “Tree” model?