You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
A collectable Coins system but it is very laggy!
What is the issue?
The issue is when ever I go near the coin it lags!
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)
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
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
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)
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)
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?