Runservice is a bit laggy

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

A collectable Coins system but it is very laggy!

  1. What is the issue?

The issue is when ever I go near the coin it lags!

  1. What solutions have you tried so far?

Finding a solution

local runservice = game:GetService("RunService")
local Player = game.Players.LocalPlayer

runservice.RenderStepped:Connect(function()
	if Player.Character:FindFirstChild("Humanoid") then
		if Player.Character.Humanoid.Health ~= 0 then
			if Player.Character.Humanoid.WalkSpeed > 0 then
				local CoinsAvailable = {}
				for i,v in pairs(game.Workspace.Collectors:GetChildren()) do
					local mag = (v.Position-Player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
					if mag <= script.MaxMagnitude.Value then
						CoinsAvailable[#CoinsAvailable+1] = v.Name
					end
				end
				if #CoinsAvailable == 1 then
					local Egg = workspace.Collectors:FindFirstChild(CoinsAvailable[1])
					for i,v in pairs(game.Workspace.Collectors:GetChildren()) do
						if v.Name == "Coin" then
							v.Touched:Connect(function(p)
								if p.Parent:FindFirstChild("Humanoid") ~= nil then
									local player = game.Players:GetPlayerFromCharacter(p.Parent)

									if v.Transparency == 0 then
										game.ReplicatedStorage.CoinQuest:FireServer(player)
										game.ReplicatedStorage.Coin:FireServer(player)
										v.Transparency = 1
										v.Sparkles.Enabled = false
										v.PointLight.Enabled = false
										for i2, v2 in pairs(v:GetChildren()) do
											for i3, v3 in pairs(v2:GetChildren()) do
												if v3.Name == "Particle" then
													v3.Enabled = false
												end
											end
										end
										v.ParticleEmitter.Enabled = true
										task.wait(0.4)
										v.ParticleEmitter.Enabled = false
										task.wait(3)
										v.Transparency = 0
										v.Sparkles.Enabled = true
										v.PointLight.Enabled = true
										for i2, v2 in pairs(v:GetChildren()) do
											for i3, v3 in pairs(v2:GetChildren()) do
												if v3.Name == "Particle" then
													v3.Enabled = true
												end
											end
										end
									end
								end
							end)
						end
					end
				end
			end
		end
	end
end)

Thank you!

there’s probably a lot of coins in the same spot (unsure), also you made it so if your character was atleast in the minimum distance between the collector children, it would add a specifically named coin in the coinsAvailable table which i think can cause some issues in the memory without any debounce whatsoever

which is why when you use runservice, it’s basically the fastest service besides task.wait() so you’d be slapped with repeating lag

Okay so i changed the script to this but it is still lagging.

local runservice = game:GetService("RunService")
local Player = game.Players.LocalPlayer

runservice.RenderStepped:Connect(function()
	if Player.Character:FindFirstChild("Humanoid") then
		if Player.Character.Humanoid.Health ~= 0 then
			if Player.Character.Humanoid.WalkSpeed > 0 then
				for i,v in pairs(game.Workspace.Collectors:GetChildren()) do
					if v.Name == "Coin" then
						v.Touched:Connect(function(p)
							if p.Parent:FindFirstChild("Humanoid") ~= nil then
								local player = game.Players:GetPlayerFromCharacter(p.Parent)
								if v.Transparency == 0 then
									game.ReplicatedStorage.CoinQuest:FireServer(player)
									game.ReplicatedStorage.Coin:FireServer(player)
									v.Transparency = 1
									v.Sparkles.Enabled = false
									v.PointLight.Enabled = false
									for i2, v2 in pairs(v:GetChildren()) do
										for i3, v3 in pairs(v2:GetChildren()) do
											if v3.Name == "Particle" then
												v3.Enabled = false
											end
										end
									end
									v.ParticleEmitter.Enabled = true
									task.wait(0.4)
									v.ParticleEmitter.Enabled = false
									task.wait(3)
									v.Transparency = 0
									v.Sparkles.Enabled = true
									v.PointLight.Enabled = true
									for i2, v2 in pairs(v:GetChildren()) do
										for i3, v3 in pairs(v2:GetChildren()) do
											if v3.Name == "Particle" then
												v3.Enabled = true
											end
										end
									end
								end
							end
						end)
					end
				end
			end
		end
	end
end)

could you show me a video? also can you elaborate on some of the code lines, theres a lot of non-summarized lines which are pretty complex to try figuring out on my own

Roblox (gyazo.com)

by the way i asked you a question earlier, can you look at the explorer and see how much coins are in the same position only?

Every coin is a different positions

oh right, i found the issue now, there aren’t any debounce to not trigger the function again after it has been triggered, try adding a temporary variable and make the function check if the variable is false or not then make it true in the next line

I am be wrong but if you add a debounce it might fix it because you may be firing a TON of the functions? EDIT: I responded a second to late lol.

Because you’re using FindFirstChild in a loop which I wouldn’t recommend, try changing that

I added a debounce but it is still lagging!

local runservice = game:GetService("RunService")
local Player = game.Players.LocalPlayer

local debounce = true

runservice.RenderStepped:Connect(function()
	if Player.Character:FindFirstChild("Humanoid") then
		if Player.Character.Humanoid.Health ~= 0 then
			if Player.Character.Humanoid.WalkSpeed > 0 then
				for i,v in pairs(game.Workspace.Collectors:GetChildren()) do
					if v.Name == "Coin" then
						if debounce == true then
							debounce = false
							task.wait(0.01)
							debounce = true
							v.Touched:Connect(function(p)
								if p.Parent:FindFirstChild("Humanoid") ~= nil then
									local player = game.Players:GetPlayerFromCharacter(p.Parent)
									if v.Transparency == 0 then
										game.ReplicatedStorage.CoinQuest:FireServer(player)
										game.ReplicatedStorage.Coin:FireServer(player)
										v.Transparency = 1
										v.Sparkles.Enabled = false
										v.PointLight.Enabled = false
										for i2, v2 in pairs(v:GetChildren()) do
											for i3, v3 in pairs(v2:GetChildren()) do
												if v3.Name == "Particle" then
													v3.Enabled = false
												end
											end
										end
										v.ParticleEmitter.Enabled = true
										task.wait(0.4)
										v.ParticleEmitter.Enabled = false
										task.wait(30)
										v.Transparency = 0
										v.Sparkles.Enabled = true
										v.PointLight.Enabled = true
										for i2, v2 in pairs(v:GetChildren()) do
											for i3, v3 in pairs(v2:GetChildren()) do
												if v3.Name == "Particle" then
													v3.Enabled = true
												end
											end
										end
									end
								end
							end)
						end
					end
				end
			end
		end
	end
end)

It is making my game lag a lot I really need help on fixing this script!

Maybe try this?

local runservice = game:GetService("RunService")
local Player = game.Players.LocalPlayer

local debounce = false

runservice.RenderStepped:Connect(function()
	if debounce == false then
		debounce = true
		if Player.Character:FindFirstChild("Humanoid") then
			if Player.Character.Humanoid.Health ~= 0 then
				if Player.Character.Humanoid.WalkSpeed > 0 then
					for i,v in pairs(game.Workspace.Collectors:GetChildren()) do
						if v.Name == "Coin" then
							v.Touched:Connect(function(p)
								if p.Parent:FindFirstChild("Humanoid") ~= nil then
									local player = game.Players:GetPlayerFromCharacter(p.Parent)
									if v.Transparency == 0 then
										game.ReplicatedStorage.CoinQuest:FireServer(player)
										game.ReplicatedStorage.Coin:FireServer(player)
										v.Transparency = 1
										v.Sparkles.Enabled = false
										v.PointLight.Enabled = false
										for i2, v2 in pairs(v:GetChildren()) do
											for i3, v3 in pairs(v2:GetChildren()) do
												if v3.Name == "Particle" then
													v3.Enabled = false
												end
											end
										end
										v.ParticleEmitter.Enabled = true
										task.wait(0.4)
										v.ParticleEmitter.Enabled = false
										task.wait(30)
										v.Transparency = 0
										v.Sparkles.Enabled = true
										v.PointLight.Enabled = true
										for i2, v2 in pairs(v:GetChildren()) do
											for i3, v3 in pairs(v2:GetChildren()) do
												if v3.Name == "Particle" then
													v3.Enabled = true
												end
											end
										end
									end
								end
							end)
						end
					end
				end
			end
		end
		task.wait(0.5)
		debounce = false
	end
end)

Now it doesn’t work when I touch the coin.

My bad change local debounce = true to local debounce = false in the beginning of the script. I edited my previous reply.

It still keeps on lagging in the server

It starts randomly giving the player coins and after a while it starts to lag, is there any way to make it have less lag

Why the hell are you putting a .Touched event inside of a RenderStepped event??? No wonder your game is lagging:

local collectors = workspace:WaitForChild("Collectors")
for _, v in pairs(collectors:GetChildren()) do
    if v.Name == "Coin" and v:IsA("BasePart") then
        v.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then

                -- This is all your code now:
                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if v.Transparency == 0 then
					game.ReplicatedStorage.CoinQuest:FireServer(player)
					game.ReplicatedStorage.Coin:FireServer(player)
					v.Transparency = 1
					v.Sparkles.Enabled = false
					v.PointLight.Enabled = false
					for i2, v2 in pairs(v:GetChildren()) do
						for i3, v3 in pairs(v2:GetChildren()) do
							if v3.Name == "Particle" then
								v3.Enabled = false
							end
						end
					end
					v.ParticleEmitter.Enabled = true
					task.wait(0.4)
					v.ParticleEmitter.Enabled = false
					task.wait(30)
					v.Transparency = 0
					v.Sparkles.Enabled = true
					v.PointLight.Enabled = true
					for i2, v2 in pairs(v:GetChildren()) do
						for i3, v3 in pairs(v2:GetChildren()) do
							if v3.Name == "Particle" then
								v3.Enabled = true
							end
						end
					end
                end
            end
        end)
    end
end

I am on mobile rn so I can’t test, but would this make it loop so basically I have like a folder with coins in if a player steps on one it should work for that one?

Looks like you’ve got this one m8, good luck