Collectionservice only affects one part

  1. What do you want to achieve? coin rotates and adds value when touched

  2. What is the issue? collectionservice only affects one part

  3. What solutions have you tried so far? well i looked for solutions on devforum but i cant seem to find one

local collectionService = game:GetService("CollectionService")

local taggedParts = collectionService:GetTagged("Coin")

local connections = {}

local function rotatePart(coin)
	while wait(.01) do
		coin.Rotation += Vector3.new(0,0,4)
	end
end

local function makeCoin(part)
	
	local connection = part.Touched:Connect(function(hit)
		local h = hit:FindFirstAncestorWhichIsA("Model")
		if h then
			local humanoid = h:FindFirstChild("Humanoid")
			if humanoid then
				local player = game.Players:GetPlayerFromCharacter(h)
				player:FindFirstChild("Data"):WaitForChild("Coins").Value += 5
			end
		end
	end)
	connections[part] = connection
end

for _, coin in pairs(taggedParts) do
	makeCoin(coin)
	rotatePart(coin)
end

script doesnt show any errors

Here, the rotatePart() function is called on the first coin in the list, and then it gets stuck there rotating the coin because it is an infinite loop of rotating the one coin. Because of this, the rest of the script won’t execute. The first solution I could think of might be to use a coroutine to run the rotatePart() function at the same time as the rest of the script.

https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine

u can just put a script in every coin with a tweenservice and make the coin rotate with it
example:

TweenService = game:GetService("TweenService")

local timetospin = 3 --put the time you want me to turn around

------------------Change the first number for the time to rotate the coin

spininfo = TweenInfo.new(timetospin ,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

Spin1 = TweenService:Create(script.Parent,spininfo,{CFrame = script.Parent.CFrame * CFrame.Angles(0,math.rad(120),0)})

Spin2 = TweenService:Create(script.Parent,spininfo,{CFrame = script.Parent.CFrame * CFrame.Angles(0,math.rad(240),0)})

Spin3 = TweenService:Create(script.Parent,spininfo,{CFrame = script.Parent.CFrame * CFrame.Angles(0,math.rad(360),0)})

Spin1:Play()

Spin1.Completed:Connect(function()Spin2:Play() end)

Spin2.Completed:Connect(function()Spin3:Play() end)

Spin3.Completed:Connect(function()Spin1:Play() end)

sorry for the edits