How to make parts fall within the radius of the player?

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. :smiley:

3 Likes

Try moving it to StarterCharacterScripts. The head is only defined once which means that if the character resets, it will not update.

3 Likes

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.

2 Likes

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.

3 Likes

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.

2 Likes

That sometimes happens to client-owned parts for unknown causes.

Does this still happen to you or is this one fixed?

3 Likes

I’m not sure about the gradual part but yeah, some parts will spawn at a really far away position on the Y axis such as around -500

Is there any way I can fix this?

2 Likes

Try setting the Y value to a fixed value.

Unless you redid your rain system for some possibly better method, no.

3 Likes

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?

1 Like

The loop is probably fine. The reason for them probably stopping randomly is either:

  • something else is freezing them
  • it left the SimulationRadius
1 Like

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.

1 Like

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.

2 Likes

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! :smiley:

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.