I made a disappearing stairs script but when I added sound, it broke the code. The reason I think it broke the code is because not every stair has sound in it. Is it possible to check if the staircase has a sound object inside it before attempting to play and if it doesn’t it just moves on and makes the stair disappear?
for i,stair in pairs(game.Workspace.Stairs:GetChildren()) do
local sound = stair.Sound
local ogSize = stair.Size
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
0.8, -- The time the tween takes to complete
Enum.EasingStyle.Linear, -- The tween style.
Enum.EasingDirection.InOut, -- EasingDirection
0, -- How many times you want the tween to repeat. If you make it less than 0 it will repeat forever.
false, -- Reverse
0 -- Delay
)
local Tween = TweenService:Create(stair, tweenInfo, {Size = Vector3.new(0,0,0)})
local Tween2 = TweenService:Create(stair, tweenInfo, {Transparency = 1})
local Tween3 = TweenService:Create(stair, tweenInfo, {Size = ogSize})
local Tween4 = TweenService:Create(stair, tweenInfo, {Transparency = 0})
function onTouched(hit)
sound:Play()
wait(0.05)
Tween:Play()
Tween2:Play()
Tween.Completed:Wait()
wait(5)
Tween3:Play()
Tween4:Play()
Tween3.Completed:Wait()
end
stair.Touched:connect(onTouched)
end
I am working this out for you and so far I’ve found something of a fix.
for i,stair in pairs(game.Workspace.Stairs:GetChildren()) do
local ogSize = stair.Size
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
0.8, -- The time the tween takes to complete
Enum.EasingStyle.Linear, -- The tween style.
Enum.EasingDirection.InOut, -- EasingDirection
0, -- How many times you want the tween to repeat. If you make it less than 0 it will repeat forever.
false, -- Reverse
0 -- Delay
)
local Tween = TweenService:Create(stair, tweenInfo, {Size = Vector3.new(0,0,0)})
local Tween2 = TweenService:Create(stair, tweenInfo, {Transparency = 1})
local Tween3 = TweenService:Create(stair, tweenInfo, {Size = ogSize})
local Tween4 = TweenService:Create(stair, tweenInfo, {Transparency = 0})
function onTouched(hit)
if stair:FindFirstChild('Sound') then
stair.Sound:play()
end
wait(0.05)
Tween:Play()
Tween2:Play()
Tween.Completed:Wait()
stair.CanTouch = false
wait(5)
Tween3:Play()
Tween4:Play()
Tween3.Completed:Wait()
stair.CanTouch = true
end
stair.Touched:connect(onTouched)
end
I have removed the local establishing the sound, line 2.
And after function onTouched(hit), I removed sound:Play(), then replaced it with a little FindFirstChild check.
I have also added lines that disable and enable CanTouch after the tweens complete, so the sound doesn’t get played if the player manages to touch the stair after it disappears.
I have very little scripting knowledge, as I am a beginner, so this is also a learning experience for me, so far this seems to work in testing.