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:
(line 50 is the line inside RunService function)
Thanks for help!
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
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 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.
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.
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)