My script is very laggy

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a game where you battle people using different powers.
  2. What is the issue? Include screenshots / videos if possible!
    I have created this script but it lags a lot. (There are no errors appearing)
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried searching on the dev forum for similar problems but had no luck.

If anyone could help me find out what is making this script so laggy it would be greatly appreciated.
Here’s the script:

script.Parent.OnServerEvent:Connect(function(plr,strike,Size,segements)
	local val = Instance.new("Weld",plr)
	val.Name = "Attacking"
	local Folder = Instance.new("Folder",workspace)
	Folder.Name = "Folder"
	
	local function GetTouchingParts(part)
		local connection = part.Touched:Connect(function() end)
		local results = part:GetTouchingParts()
		connection:Disconnect()
		return results
	end
	
	local hum = plr.Character:WaitForChild('Humanoid')
	local humRP = plr.Character:WaitForChild('HumanoidRootPart')
	
	
	
	

	local ts = game:GetService("TweenService")
	
	local radius = 5
	local Char = plr.Character
	
	
	wait()
	--What happens
	pcall(function()	
		
		local Balls = Instance.new("Model")
		Balls.Parent = game.Workspace
		Balls.Name = "Balls"
		game.Debris:AddItem(Balls,10)

		local humRp = Char:FindFirstChild("HumanoidRootPart")
		local hum = Char:FindFirstChild("Humanoid")

		if not humRp or not hum then return end

		local ray = Ray.new(humRp.Position,humRp.CFrame.LookVector * 50)
		
		for i = 1,23,1 do
			
			local circle = Instance.new("Part",Balls) circle.BrickColor = BrickColor.new("Eggplant")
			
		
			circle.Shape = Enum.PartType.Ball
			circle.Transparency = 0.5
			circle.Position = humRp.Position + Vector3.new(math.random(-7,7),math.random(3,6),math.random(-7,7))
			circle.Anchored = true
			circle.CanCollide = false
			circle.Size = Vector3.new(radius,radius,radius)
			circle.Material = "Neon"
			circle.BottomSurface = "Smooth"
			circle.TopSurface = "Smooth"
			spawn(function()
				ts:Create(circle,TweenInfo.new(.2,Enum.EasingStyle.Quad),{Position = circle.Position + Vector3.new(0,1,0)}):Play()
					wait(1)
					Play(script.Parent.Sfx.Woosh,humRp)
				ts:Create(circle,TweenInfo.new(.1,Enum.EasingStyle.Quad),{Transparency = 0.3, Position = humRp.Position+ humRp.CFrame.LookVector * 50 + Vector3.new(math.random(-7,7),0,math.random(-7,7))}):Play()
					wait(.1)
					Play(script.Parent.Sfx.Blow,circle)
					ts:Create(circle,TweenInfo.new(.2,Enum.EasingStyle.Quad),{Size = Vector3.new(radius*3,radius*3,radius*3), Transparency = 1}):Play()
					local parts = GetTouchingParts(circle)
					for i,v in pairs(parts)do
						if v.Parent:FindFirstChild("Humanoid")then
							v.Parent:FindFirstChild("Humanoid"):TakeDamage(.5)
						end
					end
					local rayParams = RaycastParams.new()
					rayParams.FilterDescendantsInstances = {circle}
					rayParams.FilterType = Enum.RaycastFilterType.Blacklist
					local ray = workspace:Raycast(circle.Position,circle.Position + Vector3.new(0,-7,0),rayParams)
					if ray then
						local part = Instance.new("Part",Folder)
						part.Size = Vector3.new(1,1,1)
						part.Anchored = false
						part.Position = circle.Position
						part.Material = ray.Material
						part.BrickColor = ray.Instance.BrickColor
						
						
						
						spawn(function()
							wait(2)
							ts:Create(part,TweenInfo.new(.2),{Transparency = 1}):Play()
						end)
					end
				wait(.3)
					
			end)
			wait(.1)
				

		end
			wait(1)
			local circle = Instance.new("Part",Balls) circle.BrickColor = BrickColor.new("Eggplant")
			circle.Shape = Enum.PartType.Ball
			circle.Transparency = 0.5
			circle.Position = humRp.Position + Vector3.new(math.random(-7,7),math.random(3,6),math.random(-7,7))
			circle.Anchored = true
			circle.CanCollide = false
			circle.Size = Vector3.new(radius*4,radius*4,radius*4)
			circle.Material = "Neon"
			circle.BottomSurface = "Smooth"
			circle.TopSurface = "Smooth"
			spawn(function()
				ts:Create(circle,TweenInfo.new(.2,Enum.EasingStyle.Quad),{Position = circle.Position + Vector3.new(0,1,0)}):Play()
				wait(1)
				Play(script.Parent.Sfx.Woosh,humRp)
				ts:Create(circle,TweenInfo.new(.1,Enum.EasingStyle.Quad),{Transparency = 0.3, Position = humRp.Position+ humRp.CFrame.LookVector * 50 + Vector3.new(math.random(-7,7),0,math.random(-7,7))}):Play()
				wait(.1)
				Play(script.Parent.Sfx.Blow,circle)
				ts:Create(circle,TweenInfo.new(.8,Enum.EasingStyle.Quad),{Size = Vector3.new(radius*7,radius*7,radius*7), Transparency = 1}):Play()
				
				
			local parts = GetTouchingParts(circle)
			for i,v in pairs(parts)do
				if v.Parent:FindFirstChild("Humanoid")then
					v.Parent:FindFirstChild("Humanoid"):TakeDamage(1)
				end
				end
				wait(1.6)
			end)
			
		
		
	end)
	
	
	
	
	wait(3.9)
	val:Destroy()
	
	Folder:Destroy()
	
end)

You shouldn’t parent each instance right after their creation, instead only parent it when you are done setting their properties.

Why make a function that already exists in Roblox? Also, every time this RemoteEvent is fired, this function is created. Preferably I would choose to make this function outside of the scope so I can reuse it instead of creating one every time.

Another bad practice, you should get the service at the beginning of the script.

I don’t understand why you would refer the character after you referred two instances that requires the reference of the character. This should be before you refer the humanoid and the humanoid root part.

Outdated, use task.wait() instead.

Parent after properties are set.

You already referred those.

Again, parent after properties are set.

Outdated, use task.spawn() instead.

This whole code is so messy.

Overall, this script is so de optimized and there’s a lot of things you need to fix.

1 Like

Hello, Thank you for your reply. This helped me a lot and I will remember this information when creating different scripts. Although I have 1 question, What is the purpose of

task.wait()

instead of

wait()

?

task.wait is part of the new task library, it’s basically the same as regular wait but better. The default wait function is planned to be deprecated so you should stop using it

1 Like

Just a small tidbit, but ItzBloxyDev is right to use this custom function the way he is.

> local function GetTouchingParts(part)
> 		local connection = part.Touched:Connect(function() end)
> 		local results = part:GetTouchingParts()
> 		connection:Disconnect()
> 		return results
> 	end

If the part that is being tested has CanCollide set to false, then you need to create a TouchIntreset with the Touched event. Otherwise, :GetTouchingParts() won’t work.

Use workspace:GetPartsInPart() instead, its the newer version of GetTouchingParts and doesn’t care about CanCollide being true or false, meaing you dont need to create a TouchInterest.

1 Like