Lagging problems with game

Yes i used alignorientation and i have enabled the RigidityEnabled property because responsiveness doesnt work at all

Try adjust adjusting to be really high. What are the values for those right now? Can you screen capture it?

Thats the script:

local previouspart = nil
local distance = 2
local numberofballs = 20
local total = numberofballs
local hrp
local char = game.Players.LocalPlayer.Character
local runservice = game:GetService("RunService")
local db = true
local limit = 400
local part
local goal
local mainpart

wait()

for i = 1,numberofballs,1 do

	part = Instance.new("Part",game.Workspace)
	part.Anchored = false
	part.Material = Enum.Material.SmoothPlastic
	part.Shape = "Ball"
	part.BrickColor = BrickColor.random()
	part.CanCollide = false
	part.Massless = true

	hrp = char.HumanoidRootPart
	
	
	if i == 1 then
		local weld = Instance.new("WeldConstraint",part)
		weld.Part0 = part
		weld.Part1 = hrp
		
		part.Position = hrp.Position - hrp.CFrame.LookVector*2
		part.CFrame = CFrame.new(part.Position,hrp.Position)
		
		part.Name = "Part1"
		mainpart = part
	else
		pos = game.Workspace:FindFirstChild("Part"..i-1).Position - game.Workspace:FindFirstChild("Part"..i-1).CFrame.LookVector*Vector3.new(1,0,1)*distance
		part.Position = pos
		
		local bodyposition = Instance.new("AlignPosition",part)
		bodyposition.RigidityEnabled = true
		
		local bodyorientation = Instance.new("AlignOrientation",part)
		
		bodyorientation.Attachment0 = Instance.new("Attachment",part)
		bodyposition.Attachment0 = Instance.new("Attachment",part)
		
		local att1 = Instance.new("Attachment",game.Workspace:FindFirstChild("Part"..i-1))
		att1.WorldPosition = att1.Parent.Position - att1.Parent.CFrame.LookVector*2
		
		bodyorientation.Attachment1 = att1
		
		bodyposition.Attachment1 = att1
		
		part.Anchored = false
		part.Name = "Part"..i
	end


	previouspart = part

	wait()
end

wait(1)

spawn(function()
	while true do

		for i, v in pairs(game.Workspace:GetChildren()) do

			if game.ReplicatedStorage.food:FindFirstChild(v.Name) then

				v.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") and db then

						print("length:"..total+1)
						db = false

						v:Destroy()

						part = Instance.new("Part",game.Workspace)
						part.Anchored = false
						part.Material = Enum.Material.SmoothPlastic
						part.Shape = "Ball"
						part.BrickColor = BrickColor.random()
						part.CanCollide = false
						part.Massless = true
						part.Position = game.Workspace:FindFirstChild("Part"..numberofballs).Position - game.Workspace:FindFirstChild("Part"..numberofballs).CFrame.LookVector*Vector3.new(1,0,1)*distance
						part.Name = "Part"..numberofballs+1

						numberofballs += 1
						
						local bodyposition = Instance.new("AlignPosition",part)
						local bodyorientation = Instance.new("AlignOrientation",part)

						bodyorientation.Attachment0 = Instance.new("Attachment",part)
						bodyposition.Attachment0 = Instance.new("Attachment",part)

						local att1 = Instance.new("Attachment",game.Workspace:FindFirstChild("Part"..numberofballs-1))
						att1.WorldPosition = att1.Parent.Position - att1.Parent.CFrame.LookVector*2

						bodyorientation.Attachment1 = att1
						bodyposition.RigidityEnabled = true

						bodyposition.Attachment1 = att1
						
						wait(0.1)
						db = true

					end
				end)
			end

		end
		wait()
	end
end)

There’s various approaches when it comes down to your provided code. Something I could suggest is a more efficient data structure Instead of using a loop to update the Position of each part in the snake, use a linked list. Or use Physics Constraints which was my approach and allow the parts to move more “realistic”. You could also Limit the size of the snake.

Efficient Data Structure

local snake = {}

for i = 1,numberofballs do
    local part = Instance.new("Part",game.Workspace)
    part.Anchored = true
    part.Material = Enum.Material.SmoothPlastic
    part.Shape = "Ball"
    part.BrickColor = BrickColor.random()
    part.CanCollide = false
    part.Massless = true
    part.Name = "Part"..i
    if i == 1 then
        part.Position = hrp.Position - hrp.CFrame.LookVector*2
    else
        part.Position = snake[i-1].Position - snake[i-1].CFrame.LookVector*distance
    end
    table.insert(snake, part)
end

Physics Constraint Method

local snake = {}

for i = 1,numberofballs do
    local part = Instance.new("Part",game.Workspace)
    part.Anchored = false
    part.Material = Enum.Material.SmoothPlastic
    part.Shape = "Ball"
    part.BrickColor = BrickColor.random()
    part.CanCollide = false
    part.Massless = false
    part.Name = "Part"..i
    if i == 1 then
        part.Position = hrp.Position - hrp.CFrame.LookVector*2
    else
        local constraint = Instance.new("BallSocketConstraint", part)
        constraint.Attachment0 = snake[i-1].Attachment
        constraint.Attachment1 = part.Attachment
    end
    local attachment = Instance.new("Attachment", part)
    attachment.Position = Vector3.new(0, 0, -part.Size.Z/2)
    table.insert(snake, part)
end

Limit Snake Size

local max_snake_size = 100
local min_snake_size = 5

local snake = {}
local previous_part = nil
local distance = 2

-- Create initial snake
for i = 1, min_snake_size do
    local part = Instance.new("Part", game.Workspace)
    part.Anchored = true
    part.Material = Enum.Material.SmoothPlastic
    part.Shape = "Ball"
    part.BrickColor = BrickColor.random()
    part.CanCollide = false
    part.Massless = true
    part.Name = "Part"..i
    
    if i == 1 then
        part.Position = game.Players.LocalPlayer.Character.HumanoidRootPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame.LookVector * 2
    else
        part.Position = previous_part.Position - previous_part.CFrame.LookVector * distance
    end
    
    previous_part = part
    table.insert(snake, part)
end

-- Food collision detection
for _, food in ipairs(game.Workspace:GetChildren()) do
    if food.Name == "Food" then
        food.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                if #snake < max_snake_size then
                    -- Add new part to snake
                    local new_part = Instance.new("Part",game.Workspace)
                    new_part.Anchored = true
                    new_part.Material = Enum.Material.SmoothPlastic
                    new_part.Shape = "Ball"
                    new_part.BrickColor = BrickColor.random()
                    new_part.CanCollide = false
                    new_part.Massless = true
                    new_part.Name = "Part"..(#snake+1)
                    new_part.Position = snake[#snake].Position - snake[#snake].CFrame.LookVector * Vector3.new(1,0,1) * distance
                    table.insert(snake, new_part)
                    
                    -- Destroy food
                    food:Destroy()
                end
            end
        end)
    end
end

I am gonna try to figure it out on my own.Thanks for your help guys

1 Like