Need help with performance issue

How would I go about optimizing this script better to do as I intend? (To clarify, I intend for the part to always spawn under the player, even when they run… to be used sort of as a path… But when the player begins to run, the parts don’t spawn right under at all times.)

I tried fixing this issue by making the part always spawn in front of the player, but soon figured out that the part doesn’t always spawn in front of the player, which I also haven’t been able to fix.

local Debris = game:GetService("Debris")

game.ReplicatedStorage.SpawnPart.OnServerEvent:Connect(function(plr)
    local hum = plr.Character:WaitForChild("Humanoid")
    hum.WalkSpeed = 32
    for i = 50, 0, -0.5 do
        local part = game.ReplicatedStorage.ICEPART:Clone()
        part.Position = plr.Character.HumanoidRootPart.Position + Vector3.new(0, -3, 6)
        part.Parent = workspace
        Debris:AddItem(part, 1) 

        task.wait()
    end
    hum.WalkSpeed = 16
    task.wait()
end)
4 Likes

Try using RunService instead of waiting for the event to happen. This will help keep the game running smooth and should work more efficiently.

4 Likes

Yeah def. It would be way more efficient than constantly sending remotes

2 Likes

Unless it is critical that this runs on server side you should run this as a localscript, the way its currently set up it will basically break the server if multiple players get close to each other and every player is spawning new objects on every frame if ran as server script.

You should avoid using debris as this takes away control of objects once they are added to debris, you should instead manage a pool of objects and by looking at the code you should then implement the two following rules to manage objects.

  • If no objects within some radius of player is less than some value spawn object at position in front of player and add object to cache.
  • Remove objects from cache if distance from player greater than some value

This would eliminate any object flickering, remove trailing objects once you reach a certain distance (instead of objects just disappearing after a certain time which would be 10 seconds by default by using debris)

1 Like