Why is my game laggy no matter what device you play on?

Everything in my game lags for some reason. Their are swords, and when you click they attack nearly 1 second after you click. Even the super simple scripts lag, like the shadow stone. If you click, it doesn’t go invisible for about a second(script shown below).


local debounce = true

script.Parent.Activated:Connect(function()
	if debounce == true then
		debounce = false
		if script.Parent.Parent.ClassName == "Model" then
			character = script.Parent.Parent
		else
			character = script.Parent.Parent.Parent.Character
		end
		
		for i,v in pairs(character:GetChildren()) do
			script.Parent.Handle.Transparency = 1
			if v.ClassName == "Part" and v.Name ~= "HumanoidRootPart" then
				v.Transparency = 1
			end
		end
		wait(5)
		for i,v in pairs(character:GetChildren()) do
			script.Parent.Handle.Transparency = .15
			if v.ClassName == "Part" and v.Name ~= "HumanoidRootPart" then
				v.Transparency = 0
			end
		end
		wait(10)
		debounce = true
	end
end)
1 Like

It depends on everything you add to your game as it will weigh the performance caused by having many models and many exaggerated hectaries of the terrain or if the models are not well created
I hope you take it into account

Everything in the game is made of blocks. Their aren’t any builds that are too big.

Sometimes you lag if lets say for instance a script has this code:

while true do
    print("This print statement will spam and create lag")
end

Then the print statement WILL spam and create lag. But, if it was like this:

while true do
    print("This print statement won't spam since the line below this is a wait")
    wait(1)
end

Then it won’t be laggy.

Other than that, I don’t know what else to say.

Hello! You should change your topic category for #help-and-feedback:scripting-support :slightly_smiling_face:

Also, your script doesn’t work because you are checking "if v.ClassName == "Part" then " while all parts of the character are “MeshPart”!

So there is a basic solution:

First, let’s define the variables:

local Player = game:GetService("Players").LocalPlayer --> The local Player

local Button = script.Parent
local Debounce = true
local toIgnore = "HumanoidRootPart"

Now we are going to create the functions, to simplify everything, we are going to use the variables we created. A local function is used to store functions, it is widely used to improve your code and make it cleaner.

local function ChangeTransparency(Parent, Transparency)
	for Index, Value in pairs(Parent:GetDescendants()) do
		
		--Now, `:IsA ("Class")` will check if the object belongs to "Class", if it is, it will return true, otherwise, false.
		if Value:IsA("BasePart") and Value.Name ~= toIgnore then
			Value.Transparency = Transparency
		end
	end
end

Now we are going to create the main local function, which will be called when the button is activated.

local function onButtonActivated()
	--> Here we will check if the player has a character and the debounce
	if Player.Character and Debounce then
		
		--> Player.Character just return the current character of LocalPlayer
		local Character = Player.Character
		Debounce = false
		
		--> Let's call the function, the first parameter (Character) will be called Parent, and the second, Transparency
		ChangeTransparency(Character, 1)
		wait(5) --> Time to wait!
		
		--> Let's call another time to change the transparency back
		ChangeTransparency(Character, 0)	
		wait(10) --> Wait.. zZzzzZ
		
		Debounce = true
	end
end

Lastly, let’s connect the functions, we only need to connect the function onButtonActivated to be executed when the button is pressed.

Button.Activated:Connect(onButtonActivated)

As for the fact that your game is lagged, there are many factors that can differentiate the cause of the delay, such as your game map, loops while true do, serverside :Connects() without :Disconnect().

You should evaluate it a little more and tell us specifically.

2 Likes

The script already works because the game uses r6 characters.

It might not just be the scripts, it could be based on how many parts you have.