Explanation
The script first starts playing music and waits until it reaches a certain time position in the song, then this function starts running :
function AnimateParts(Player, Radius)
EffectedObj = {}
workspace.TempParts:ClearAllChildren()
local SkyBox = Instance.new("Sky")
SkyBox.Name = "Galaxy"
SkyBox.SkyboxBk = "http://www.roblox.com/asset/?id=159454299"
SkyBox.SkyboxDn = "http://www.roblox.com/asset/?id=159454296"
SkyBox.SkyboxFt = "http://www.roblox.com/asset/?id=159454293"
SkyBox.SkyboxLf = "http://www.roblox.com/asset/?id=159454286"
SkyBox.SkyboxRt = "http://www.roblox.com/asset/?id=159454300"
SkyBox.SkyboxUp = "http://www.roblox.com/asset/?id=159454288"
SkyBox.StarCount = 0
SkyBox.CelestialBodiesShown = false
SkyBox.Parent = game:GetService("Lighting")
for i, v in pairs(game:GetService("Workspace"):GetDescendants()) do
if v:IsA("Decal") or v:IsA("Texture") then
if not v:IsDescendantOf(Player.Character) then
local Data = {
Object = v;
Transparency = v.Transparency
}
v.Transparency = 1
table.insert(EffectedObj, Data)
end
end
end
for i, v in pairs(game:GetService("Workspace"):GetDescendants()) do
if v:IsA("BasePart") and not v:IsA("Terrain") and not v:IsDescendantOf(Player.Character) then
local Data = {
Object = v;
Transparency = v.Transparency
}
v.Transparency = 1
if (Player.Character.HumanoidRootPart.Position - v.Position).magnitude <= Radius then
local Clone = v:Clone()
Clone.CanCollide = false
Clone.Transparency = 0
Clone.Parent = workspace.TempParts
local MaxRot = 20
TweenService:Create(Clone, TweenInfo.new(1.2), {
Size = v.Size/3;
Orientation = Vector3.new(math.random(0, MaxRot) + v.Orientation.X, math.random(0, MaxRot) + v.Orientation.Y, math.random(0, MaxRot) + v.Orientation.Z);
Transparency = 1
}):Play()
end
table.insert(EffectedObj, Data)
end
end
end
Basically this function finds parts in a specified radius around the player ( the part count could range from 20 - 400 parts ), and makes these parts disperse in random rotations, then they disappear. ( All of this is running inside of a local script )
Problems
The script works fine and all, but there are two issues :
- Each time the animation starts playing a few frames get skipped. In this video you can see that while I’m walking it freezes :
( This is the main problem i want to fix )
- I want to exclude all player characters from the animated parts. ( Currently I’m only excluding the local player, because I’m not sure how to exclude all players )
Ideas
I’ve got two ideas for fixing the freeze frames right now :
-
Lower the amount of parts being animated
-
Don’t use TweenService ( e.g. maybe I could try using a for loop, but I’m not sure how I would make that work )
Lowering the amount of parts
This one probably isn’t too hard to implement. The reason why I wouldn’t want to reduce the amount of parts too much, is because it won’t look the same as originally intended. If I had to implement this, I would first get a list of the parts inside of the radius, then get rid of 40% of the parts in the list. ( I’m not sure how to do this in a script though )
Avoiding TweenService
In my opinion this would be the best option. The reason for doing this would be to reduce the smoothness of the tween, so it gets updated less frequently. ( Don’t know if this would have the ultimate performance improvement though )
Of course if you have a better idea post that instead.
Addition
These might not even be the main reasons for why the script causes lag in the beginning. Maybe another option would be to preload the assets for the skybox?
If you have any other ideas on how my code could be improved please tell me!