Improving my code

I am trying to make my code more effecient and create less lag so I’d like to know how I can Improve this.

image

My guesses are that I could use renderstepped instead of a while loop.
Also this is code for making a block disappear when you step on it and then after 5 seconds reappear

2 Likes

Is there a reason why you use wait() and then further in the code task.wait()?

I recommend you use task.wait() in every code

2 Likes

Also, if you would like to know how to use a renderstepped loop,
I suggest you read this post: Is this how you do a renderstepped loop?

I however suggest you DONT use renderstepped in this case, because the code will run every frame which may result in unnecessary lag.

One more thing, you don’t need to use a while loop in this scenario, since you use the ‘Touched’ event.

Instead of using
While yes == true do
Use
If yes == true then

2 Likes

I do not consider myself to be the best at Roblox scripting, but there are some mistakes that you must avoid.

  • The variable yes doesn’t give much context or information to us when we read your code. What does this variable do? Is it important? Why is it there? Through reading your whole code thoroughly, it seems like this variable acts as a debounce. I recommend renaming this variable as debounce for better context. You can read more about debounce here.
  • This is really an inefficient way to “animate” your part to become transparent. Instead of using while loops, why not use TweenService? This service allows you to “animate” instances’ properties with only just a few functions and information to input.
  • As mentioned by @9pwnr, you should use task.wati() in favor of wait() to avoid throttling.
  • Your if control structure is not indented correctly. Starting from the FadingPart.Transparency = 0 code line, it is not indented. This can significantly reduce readability.

No. You need to be careful on when you should use RunService.RenderStepped. As outlined by Creator Hub itself, it should only be used when dealing with the player’s character or camera.

Hope my advices help.

2 Likes

Thanks for the FeedBack I appreciate it a lot! Edit: Probably should have not named that yes, but its not debounce. I put it there to end the loop so that it does not loop infinitly after it’s touched once. However I read about it and thats actually going to help A lot with some other things I’ve been scripting for cooldowns.

1 Like

Here is how I would reformat it

That’s pretty good, there’s only a few things I would change though. Reduce magic numbers and use CollectionService so you don’t need to edit >100 scripts to change one thing.

Here’s how the code would look using CollectionService:

local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")

local TWEEN_TIME = 1.5
local TOUCH_DEBOUNCE_TIME = 5

local function onFadingPartAdded(fadingPart: Instance)
	if not fadingPart:IsA("BasePart") then
		return
	end

	local canTouchDebounce = false
	local fadeTween = TweenService:Create(fadingPart, TweenInfo.new(TWEEN_TIME, Enum.EasingStyle.Linear), {
		Transparency = 1,
	})

	fadingPart.Touched:Connect(function()
		if canTouchDebounce then
			return
		end
		canTouchDebounce = true

		fadeTween:Play()
		fadeTween.Completed:Wait()
		fadingPart.CanCollide = false

		task.wait(TOUCH_DEBOUNCE_TIME)

		canTouchDebounce = false
		fadingPart.CanCollide = true
		fadingPart.Transparency = 0
	end)
end

CollectionService:GetInstanceAddedSignal("FadingPart"):Connect(onFadingPartAdded)
for _, part in CollectionService:GetTagged("FadingPart") do
	onFadingPartAdded(part)
end
1 Like

Great addition! Collection Service has been a real game changer for me!

Something to look out for however. When you’re checking if fadingPart:IsA("BasePart") you need to also check if the part:IsDescendantOf(workspace). Otherwise these tags will run even on objects in ReplicatedStorage for example.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.