Hey there. Recently I noticed in my game, a certain script called “Train Script” is showing EXTREMELY high activity rate, which is kind of wierd. Im wondering how is that possible since the script is so simple. I will leave it here
wait(0.5)
local seat = script.Parent --receive touch input
local Train = seat.Parent-- entire train
local humanoid = Train:FindFirstChild("Humanoid")
local SoundSpeaker = script.Parent.Parent:FindFirstChild("SoundSpeaker")
local debounce = true
while true do
task.wait()
local partTouching = workspace:GetPartsInPart(seat)
for _,part in pairs(partTouching) do
--Train Engine Sounds
if part:IsA("Part") and part.Name == "Decelerate" and debounce == true then
debounce = false
SoundSpeaker:WaitForChild("Motor"):Pause()
SoundSpeaker:WaitForChild("Motor2"):Pause()
SoundSpeaker:WaitForChild("Decelerate"):Play()
debounce = true
---NextStation---
elseif part:IsA("Part") and part.Name == "NextGoldCoast" and debounce == true then
debounce = false
SoundSpeaker.NextGoldCoast:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "NextIntermarium" and debounce == true then
debounce = false
SoundSpeaker.NextIntermarium:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "NextRiverIsles" and debounce == true then
debounce = false
SoundSpeaker.NextRiverIsles:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "NextCloudForest" and debounce == true then
debounce = false
SoundSpeaker.NextCloudForest:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "NextAutumnWoodland" and debounce == true then
debounce = false
SoundSpeaker.NextAutumnWoodland:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "NextBayshorePark" and debounce == true then
debounce = false
SoundSpeaker.NextBayshorePark:Play()
debounce = true
---ArrivingStationSounds---
elseif part:IsA("Part") and part.Name == "ArriveGoldCoast" and debounce == true then
debounce = false
SoundSpeaker.ArriveGoldCoast:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "ArriveIntermarium" and debounce == true then
debounce = false
SoundSpeaker.ArriveIntermarium:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "ArriveRiverIsles" and debounce == true then
debounce = false
SoundSpeaker.ArriveRiverIsles:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "ArriveCloudForest" and debounce == true then
debounce = false
--SoundSpeaker.ArriveCloudForest:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "ArriveAutumnWoodland" and debounce == true then
debounce = false
SoundSpeaker.ArriveAutumnWoodland:Play()
debounce = true
elseif part:IsA("Part") and part.Name == "ArriveBayshorePark" and debounce == true then
debounce = false
--SoundSpeaker.ArriveBayshorePark:Play()
debounce = true
---AnnouncementSounds---
elseif part:IsA("Part") and part.Name == "Announcements" and debounce == true then
debounce = false
--Attention all passengers sounds
debounce = true
--Modifiers
elseif part:IsA("Part") and part.Name == "TrainKillerA" then
debounce = false
humanoid.Health = 0
debounce = true
end
end
end
I just mean that all the task that are inside the loop and which actually performs another for loop inside the while loop, its taking time to be solved, and its a “very quick” loop, cause you are waiting the less amount of time possible for the task manager to solve that when using task.wait(), so its good, its just that the script depending on the tasks you have inside that loop could be pretty demanding, so thats why
nono, its not about that, its that you’re using a while loop (basically executes every heartbeat which isnt performant if youre doing heavy stuff in each iteration)
you can use a .Touched event instead of the while loop
you are using too much elseif, create a table of functions outside the loop and if your parts name exist in the table, fire the function. Also create a new collision group for your parts so it does not check Default group which every part is at. Also i don’t think this is causing much lag.
I think what Art is telling you, is that maybe you should think on a different approach based on Events instead of a while loop. Is there any other approach that you could use to handle this?
Nope, there is not event that fires when something is added on a table, but, if you have a function that is adding something into a table, then you know something is being added, why not creating your “own event” when you know something is added on table?
OOP… I think that maybe OP is not going into that road yet, and sorry for inferring that. I could be wrong. Suggesting OOP is probably always the best answer
Honestly thats hard to explain when I dont know your setup and system…
What I infer by a quick check of your script and what you said, is that you have a “train” that is moving with tweening which wont trigger physics events as touched.
You are solving this by constantly check (the loop) if a part named X is inside the boundaries of the train in order to fire certain behavior.
I would first suggest, why not changing the tween movement and handle it by changing its CFrame, lerping or something that can fire physics engine?
Changing from task.wait to task.wait(0.1) and it reduced it from 20% to just under 1%. Simple solution and I didnt think of it as I thought it might not work! Thank you so much and to others that help