So, I want to make a local script that spins coins. I have found multiple different ways of doing this but don’t know which is the most efficient/performant. Which of the 3 following types would be the best to use?
Type 1:
local function spinCoins()
for _, c in pairs(CollectionService:GetTagged(tag)) do
c.CFrame = c.CFrame * CFrame.Angles(0,math.rad(2),0)
end
end
RunService.RenderStepped:Connect(spinCoins)
This is the full script. Why would it be better to use Hearbeat over RenderStepped?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local SoundService = game:GetService("SoundService")
local CoinEvent = ReplicatedStorage.Remotes.Coin
local Coins = Workspace:WaitForChild("Coins")
local db, db2 = false, false
local tag = "cpart"
local player = game.Players.LocalPlayer
local function onTouch(c)
if db == false then
db = true
c:Destroy()
CoinEvent:FireServer()
SoundService.SFX.Coin:Play()
task.wait(0.5)
db = false
end
end
local coins = {}
local function spinCoins()
for _, c in pairs(CollectionService:GetTagged(tag)) do
c.CFrame = c.CFrame * CFrame.Angles(0,math.rad(2),0)
end
end
local function onAdded(object)
coins[object] = object.Touched:Connect(function(p)
if p.Parent == player.Character then
onTouch(object)
end
end)
end
local function onRemoved(object)
if coins[object] then
coins[object]:Disconnect()
coins[object] = nil
end
end
CollectionService:GetInstanceAddedSignal(tag):Connect(onAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onRemoved)
RunService.RenderStepped:Connect(spinCoins)
for _, object in pairs(CollectionService:GetTagged(tag)) do
onAdded(object)
end
RenderStepped runs before anything is rendered along with other things and should only be used for things like cameras, Heartbeat runs after the frame is rendered and is essentially a while task.wait() loop.