I have a problem with For loops. Every second it checks for a total of 40 values if they changed and when one does it sends an event to update the values on the server side. But every time it runs, the next time it runs more times to the point where there are lots of lag spikes. Here is my code client side:
local rp = game:GetService("ReplicatedStorage")
local event = rp.UpdateValues
local plr = game.Players.LocalPlayer
wait(3)
while wait(1) do
for i, v in pairs(plr:GetDescendants()) do
if v:IsA("NumberValue") or v:IsA("StringValue") and v.Parent:IsA("Folder") then
local connection
connection = v:GetPropertyChangedSignal("Value"):Connect(function()
task.wait(.1)
connection:Disconnect()
event:FireServer(v.Name, v.Value)
end)
end
end
end
and here is my code server side:
local rp = game:GetService("ReplicatedStorage")
local event = rp.UpdateValues
event.OnServerEvent:Connect(function(plr, name, value)
for _, v in pairs(plr:GetDescendants()) do
if v.Name == name then
v.Value = value
print("Name: "..v.Name..", New Value: "..tostring(v.Value))
end
end
end)
So far I have tried adding a cooldown and using the disconnect function like above but it doesnt work. I have done an hour worth of research but couldnt find anything