I am trying to make my code more effecient and create less lag so I’d like to know how I can Improve this.
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
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.
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.
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.
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
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.