Collecting coins

Hi! I made a script that tweens the coins and rotates them, watch:
robloxapp-20200417-1755453.wmv (1.1 MB)
But the problem appears when I want do delete a coin by the touch, first coin deletes its self without an error, but any further coin causes an error.
robloxapp-20200417-1746259.wmv (1.5 MB)
Here is a script:

local coins = game.Workspace.Coins:GetChildren()

for i, v in pairs(coins) do
	local tw = game:GetService("TweenService")
	local info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, math.huge, true, 0)
	local goal = {Position = v.End.Position}
	local tween = tw:Create(v.Coin, info, goal)
	tween:Play()
	v.Coin.Touched:Connect(function()
		v:Destroy()
		table.remove(coins, i)
	end)
	game:GetService("RunService").Heartbeat:Connect(function(frame)	
	for i, v in pairs(coins) do
		v.Coin.CFrame = v.Coin.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	end
end)	
end

and here is a capture of my output:
Opomba 2020-04-17 180535
(line 50 is the line inside RunService function)
Thanks for help!

Could you send a picture of the explorer to see where all the coins are located?

Opomba 2020-04-17 181056
This is inside of workspace and script is located inside of ServerScriptService

idk, but I am calling v.Coin afterall

1 Like

The code is supposed to fetch models, that’s not the issue, I guess. I think it’s fetching the wrong model. Try printing v.Name in the loop.

1 Like

at the start it prints all four parts, when I touch first coin, it print only three, as expected, but when I go for a next one it still prints three

Try

table.remove(coins, v)

Is the tween effect supposed to happen once it’s collected? If so, you might want to move the tween:Play() over into the v.Coin.Touched function

Here’s my rewritten version of your original code:

local coins = game.Workspace.Coins:GetChildren()
local tw = game:GetService("TweenService") -- There's no need to redefine this variable every time the loop happens, it's fine outside of the loop

for i, v in pairs(coins) do
	local info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, math.huge, true, 0)
	local goal = {Position = v.End.Position}
	local tween = tw:Create(v.Coin, info, goal)

	game:GetService("RunService").Heartbeat:Connect(function(frame)	
  	    for i, v in pairs(coins) do
		    v.Coin.CFrame = v.Coin.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	    end
    end)	

	v.Coin.Touched:Connect(function()
        tween:Play()
        spawn(function()
            wait(2)
		    v:Destroy()
        end)
		table.remove(coins, i)
	end)
end

robloxapp-20200417-1822410.wmv (1.3 MB)
It breaks rotation of other coins

I think I got the issue. After inserting the code specified below, my baseplate started spinning. So, basically, it is not indexing the right model, as I said in a reply above. Let me find a solution for this.

My test code:

local coins = game.Workspace.Coins:GetChildren()

for i, v in pairs(coins) do
	for i = i, table.getn(coins) do
		print(coins[i])
	end
	v.Touched:Connect(function()
		v:Destroy()
		table.remove(coins, v)
	end)
	game:GetService("RunService").Heartbeat:Connect(function(frame)	
	for i, v in pairs(coins) do
		v.CFrame = v.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	end
end)	
end

Edit: The baseplate started spinning because the test parts were welded to it, nevermind.

Try my edited version, I put the wait and destroy in a spawn function so that it hopefully won’t interrupt any other coins

I think it’s because once you remove an item from a list, all the other items shift down. Here’s an example:

local list = {"a", "b", "c"}

Above is a list that has “a”, “b”, and “c” in it.

for i, v in pairs(list) do
    table.remove(list, i)
end

The first time it runs, it removes the first one, and all the items shift downward, so the list looks like this now:

{"b", "c"}

So the next time the loop runs where i = 2, it would be removing "c", not "b"

pranvexploder, DataErased, still didnt work

Try putting the RunService function outside the loop like so:

local coins = game.Workspace.Coins:GetChildren()

for i, v in pairs(coins) do
	local tw = game:GetService("TweenService")
	local info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, math.huge, true, 0)
	local goal = {Position = v.End.Position}
	local tween = tw:Create(v.Coin, info, goal)
	tween:Play()
	v.Coin.Touched:Connect(function()
		v:Destroy()
		table.remove(coins, i)
	end)
end
game:GetService("RunService").Heartbeat:Connect(function(frame)	
	for i, v in pairs(coins) do
		v.Coin.CFrame = v.Coin.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	end
end)	

Edit: This is working for me now. If it isn’t for you, then it’s certainly an issue with the tweening, which is a rare case.

it didnt work, also I tried ur point LoveTheBears101 with replacing i with 1, but that didnt work either

I don’t know why. It is working for me. Try editing this code:

local coins = workspace.Coins:GetChildren()
local tw = game:GetService("TweenService")

for i, v in pairs(coins) do
	local info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, math.huge, true, 0)
	local goal = {Position = v.Position}
	local tween = tw:Create(v, info, goal)
	v.Touched:Connect(function()
		v:Destroy()
		table.remove(coins, i)
	end)
end

game:GetService("RunService").Heartbeat:Connect(function(frame)	
	for i, v in pairs(coins) do
		v.CFrame = v.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	end
end)

Edit: Noticed an issue, others stop rotating once one part is touched. However, this problem occurred only after I added the tweening part to my script.

Edit 2: Only the last part stops rotating, now I get the issue, it is because the wrong object is being removed from the list.

Yeah, now I’m absolutely sure that wrong object is being removed from the table. I will try to find a solution for this.

Opomba 2020-04-17 185615
so here I printed v (2) and index (1) so that means my coin was successfully removed from the table

Perfect. That was real quick but yes, I found the solution.
I believe this should be your code. I may have made mistakes in adding the .Coins and .End, hope you rectify it.

local coins = workspace.Coins:GetChildren()
local tw = game:GetService("TweenService")
local info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, math.huge, true, 0)

for i, v in pairs(coins) do
	local goal = {Position = v.End.Position}
	local tween = tw:Create(v.Coin, info, goal)
	v.Touched:Connect(function()
		v:Destroy()
		wait()
		coins = workspace.Coins:GetChildren()
	end)
end

game:GetService("RunService").Heartbeat:Connect(function(frame)	
	for i, v in pairs(coins) do
		v.Coin.CFrame = v.Coin.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	end
end)

That’s interesting, watch this output:
Opomba 2020-04-17 190257
first it detects an error but then suddenly keeps going correctly and removes coing from the table, this pattern appears at any further coin