Hello, I am trying to create a rain system that involves falling parts. The best way I know to reduce lag with this type of rain would be to only make it rain parts within a radius of the player, instead of parts falling on the whole map.
I attempted to get the parts to spawn within a radius of the player, however, for some reason the raindrop parts will gradually begin to spawn at a really far away position and I don’t know why.
Here is the script, a local script located in player scripts:
local RepStorage = game.ReplicatedStorage
local RainStuffFolder = script.Parent
local RainFolder = game.Workspace.RainFolder
local RainPart = RainFolder.RainPart
local RainDrop = RepStorage.RainDrop
local Player = game.Players.LocalPlayer
local Head = Player.Character.Head
local Radius = 35
-- RainDrops
while wait() do
local X = math.random(Head.Position.X-Radius,Head.Position.X+Radius)
local Y = RainPart.Position.Y + math.random(-RainPart.Size.Y + Radius,RainPart.Size.Y + Radius)
local Z = math.random(Head.Position.Z-Radius,Head.Position.Z+Radius)
local RainDropClone = RainDrop:Clone()
RainDropClone.Parent = RainFolder.RainDrops
RainDropClone.Position = Vector3.new(X,Y,Z)
end
local Drops = RainFolder.RainDrops:GetChildren()
Drops.Touched:Connect(function()
Drops:Destroy()
end)
If I made anything unclear ask me and I’ll explain the best I can.
Hello, thanks for the response. I tried what you said and now the cloned parts aren’t being destroyed at all, and also, this script is disabled and re-enabled by another script and when in startercharacterscripts, this script can’t be access when disabled. Also, the cloned parts are now getting stuck as if they were anchored, even though they aren’t being anchored. I’m not sure why this is happening.
The cloned parts should use the Debris service. I’m not sure why the script would be being disabled, but alternatively you could make it into a character added function.
Hi, sorry I guess I wasn’t clear enough, I leave the script disabled so another script will enable it when it is time to rain. Also, thank you for telling me about Debris service, I did not know about that. I will use Debris from now on to destroy parts. Also, it seems parts are still getting stuck at a random position instead of falling as I mentioned in the first response.
Okay so the Y thing is fine now, however, I realized that the parts begin to stop spawning as often as they were when the rain first begins. I feel a bit dumb but I think the original issue was just the parts falling really far down without being destroyed, thats why the Y axis was so low, but the actual problem is that the parts just begin to stop spawning as often. Is it because I’m using a while wait() loop?
Oh sorry, I was referring to a new issue of the parts not spawning. However, with the parts freezing, how can I check to see if they have left the radius? I don’t think anything else could be causing them to freeze because I’m currently using a test place with just a baseplate and the rain system.
The calculation of the raindrop position is a bit weird. I would do it like this:
local RepStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local RainFolder = workspace.RainFolder
local RainDrop = RepStorage.RainDrop
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local Radius = 35
local Height = 50
-- RainDrops
while task.wait() do
local X = Head.Position.X + Radius*(2*math.random() - 1)
local Y = Head.Position.Y + Height
local Z = Head.Position.Z + Radius*(2*math.random() - 1)
local RainDropClone = RainDrop:Clone()
Debris:AddItem(RainDropClone, 1)
RainDropClone.Position = Vector3.new(X,Y,Z)
RainDropClone.Parent = RainFolder.RainDrops
end
You should use Debris and make sure that CanCollide is false for RainDrop.
By the way it doesn’t matter if your localscript is in StarterPlayerScripts or StarterCharacterScripts in this case.
Hey, thank you so much! With this code, I’m no longer getting any issues! Also, the other person told me about Debris Service but still thank you for letting me know as well!
In this script, it does seem to matter. If the player resets their character and the script is in PlayerScripts, the new head will not be used, and it will use the position of the old one.