Extremely High Script Activity

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


In this screenshot, you can see the activity rate reaching as high as 10.

Can someone help me review my script and see what is going on?

3 Likes

what do you mean by “simple” when I see:

while true do
	task.wait()

Meaning all the tasks inside that loop are on the limit of time possible to be solved?

2 Likes

Wait, is there a difference in meaning in wait() and task.wait()? I thought task.wait() is more effecient?

2 Likes

Its totally perfect that you used task.wait().

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

2 Likes

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

1 Like

The train is being tweeted, so a touch event would not fire

2 Likes

what should the script do? i can’t seem to understand what its trying to do

1 Like

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.

1 Like

Oh the script is for the sensor part of the train. When a part touches the sensor part, this script will tell it what to do

2 Likes

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?

Hmm, is there a event that fires everytime a part is added to a table?

2 Likes

Instead of using while true do, you can utilise invisible blocks which allow you to use .Touched


(I’ve made it semi-transparent to show what I’m talking about)

You can make this fully transparent with CanCollide off and .Touched. will still fire.

1 Like

I already did this. The seat would touch semi invisible blocks. Touch event would not fire

2 Likes

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?

1 Like

you could technically do exactly that using the __newindex metamethod (iirc doesn’t work with arrays but eh :man_shrugging:)

2 Likes

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

3 Likes

What should be the appropriate step for me to do right now? I cant think about any event that would fire when a New getPartInPart Part appears

2 Likes

you could increase the interval between iterations to be long enough to capture the events (there’s probably better and more reliable ways than this)

1 Like

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?

1 Like

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 :smiley:

2 Likes