Learning how to script. Could I simplify this script?

Hey there! Super new to scripting. However, I really want to learn. My goal is to make a simple obby and in a way make a simple script playing ground for myself within the obstacles just so I can practice.

My goal here? I am making planks that you can walk on and as soon as you touch them they become invisible to where you’d eventually fall through. The script works and is functional, but I’m curious as if I over complicated it? Could I have made this easier?

Here is the script below:

function onTouch(part)
local human = part.Parent:findFirstChild(“Humanoid”)
if (human ~= nil) then
wait(0.5)
script.Parent.Transparency = 0.25
wait(0.5)
script.Parent.Transparency = 0.5
wait(0.5)
script.Parent.Transparency = 0.75
wait(0.5)
script.Parent.Transparency = 1.0
script.Parent.CanCollide = false
wait(5)
script.Parent.CanCollide = true
script.Parent.Transparency = 0.75
wait(0.5)
script.Parent.Transparency = 0.5
wait(0.5)
script.Parent.Transparency = 0.25
wait(0.5)
script.Parent.Transparency = 0

end

end

script.Parent.Touched:connect(onTouch)

Thanks for your time!

You can use loops and basic addition/subtraction to simplify this or the way I did it (@blub20074 suggested it ) which is tweening it, it is really smooth.

local part = script.Parent
local TS = game:GetService("TweenService")

local TweenInformation

function OnTouch()
	TweenInformation = TweenInfo.new(1.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
	local goal = {}
	goal.Transparency = 1
	
	local tween1 = TS:Create(part, TweenInformation, goal)
	
	tween1:Play()
	
	part.CanCollide = false
	wait(5)
	
	TweenInformation = TweenInfo.new(1.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local goal = {}
	goal.Transparency = 0
	
	local tween2 = TS:Create(part, TweenInformation, goal)
	
	tween2:Play()
	
	part.CanCollide = true
end

part.Touched:Connect(OnTouch)
3 Likes

I don’t know if you already learned but using tweenservice would be the best because it would be smoother (looking to your script I think you will understand the tutorial from devking)

2 Likes

Actually, that would work, let me adapt my script to me.

1 Like

Yea I’m on phone now so I can’t write it