Hundreds of parts cause a LOT of lag

So my game clones heads. It’s the main purpose of the game. However, since people can get hundreds and thousands of heads cloned, eventually, the game beings to lag a lot. I was wondering if there was a way to make it so there is no lag even with a lot of parts inside the game.

4 Likes

Try adding the heads on all Clients (Not the server)

2 Likes

The issue is I want every player to see everybody else’s heads that have been cloned so if I do client side, no one will be able to see other players heads.

2 Likes

Use remote events to spawn them on every client’s game

1 Like

You could utilise the ‘Debris’ service to clean up old heads.

local Game = game
local Workspace = workspace
local Debris = Game:GetService("Debris")

local HeadClone = Head:Clone() --'Head' is a reference to a head.
HeadClone.CFrame = CFrame.new(0, 0, 0) --Move the head.
Debris:AddItem(HeadClone, 30) --Schedule for the head to be removed after 30 seconds.
HeadClone.Parent = Workspace
1 Like

Sorry if this is not a Script (Lighting)
You can, setting the lightings (example turn of shadows, and other shaders)
or better if the heads were not a union or meshed parts

or you can change the material to Smooth Plastic, to turn of the hidden PBR effect
or set do not use ShadowMap and Voxel.
but if you are going to use future, turn of the Light Shadows from lights (if you do have them)

2 Likes

How about creating a render distance system

1 Like

Use remotes and put everything on the clients side, to allow other players to see everyone elses heads use FireAllClients().

Could you supply some sample code? It would make it easier to help you.

1 Like
event.OnServerEvent:Connect(function(player)
	local char = player.Character
	local head = char:WaitForChild("Head")
	
	local headFolder = player.Character:WaitForChild("headContainer")
	h.Value += 1*m.Value
	
	local c = head:Clone()
	c.Parent = headFolder
	c.CanCollide = false
	c.Name = player.Name.."Head"
end)

Inside a server script

1 Like

The thing is that I already destroy the heads after they sell so

1 Like

You would do something like:

Server script:

Event.OnServerEvent:Connect(function(player)
 -- put whatever sanity checks here
    Remote:FireAllClients()
end)

On local script:

Remote.OnClientEvent:Connect(function(player)
        -- head code stuff goes here
	local character = player.Character
	local head = character:WaitForChild("Head")
	
	local headFolder = character:WaitForChild("headContainer")
	h.Value += 1*m.Value
	
	local c = head:Clone()
	c.Parent = headFolder
	c.CanCollide = false
	c.Name = player.Name.."Head"
end)

Something like this, I haven’t tested this in studio.

This way your storing the heads on all the clients individually instead of storing it on the server. Make sure you have sufficient sanity checks though so players cant exploit by firing the remotes illegally.

1 Like

Sorry for responding so late but would “Remote” be a different RemoteEvent or a RemoteFunction?

1 Like

Yes Remote is a different remote event.

You can see more on Remote events here:

Fire a remote from client to server, when server receives, it fires another remote with FireAllClients with the hard as a parameter, the client will clone the head and destroy the original one on the server

So I tried this method and these are the results…

  1. The heads only still show for the player
  2. They don’t disappear when you sell…
event.OnClientEvent:Connect(function()
	local c = head:Clone()
	c.Parent = headFolder

end)

Sell script:

			for _, v in pairs(folder:GetChildren()) do
				if v:IsA("Part") then
					v:Destroy()
				end
			end

The way your system should be set up, is every time the player wishes to perform a certain action, whether it be to sell a head, buy a head etc, you should always be telling the server to tell all other clients to also perform this action.

The reason why you would first tell the server that player1 has sold a head, is so the server can authenticate the sale, basically to check there’s no illegal business going on here (exploits), after the server has completed the necessary checks, you should then go and fire this command (which player1 initiated) to all other clients, player’s 2 and 3 etc, which would be to sell player1’s head.

Everything mentioned above can only be done using remote events, I suggest you thoroughly learn remote events to better your scripting knowledge.

So to my understanding, I need to create another remote event for selling and then fire that to then which activated the sell script?

For example:

event.OnClientEvent:Connect(function()
	for _, v in pairs(folder:GetChildren()) do
		if v:IsA("Part") then
			v:Destroy()
		end
	end
end)

Yes, player1 would press the “sell head” gui button, upon that button being clicked, you should fire a remote event to the server telling the server: player1 has attempted to sell his head! then the server will check to see if player1 is allowed to sell his head, if player1 is allowed to sell his head, then you would fire another remote (this time to all clients) telling all other players on their own clients to delete player1’s head.

It’s touch for this game but I get the point. But how would I check if the player is “allowed” to sell their head?

That depends on how your game is setup, one way could be checking if the player has enough cash to buy a head? check if the player owns a head?, I would recommend first trying to get the remote event communication working first before we go into how the server can do checks.