I’m pretty new to coding(took a long break, I was still new back then), and I’m wondering how I can improve this code? Please also explain what you’re doing and why.
if you want context, I’m making an obby for fun, it took me ~15 mins to make and script.
Instead of manually setting each Platform’s transpercy, it’s better to use a loop to loop through each platform and set its properties. Like this,
Make a folder in the workspace called “Platforms” and put all your platforms in there so we can loop through the folder
local PP = game.Workspace.Button1.ProximityPrompt
local Button1 = game.Workspace.Button1
local Baseplate = game.Workspace.Baseplate
local Platforms = workspace.Platforms
PP.Triggered:Connect(function()
Button1.BrickColor = BrickColor.Red()
for _, Platform in pairs(Platforms:GetChildren() do -- loop through every platform
Platform.Transparency = 0
Platform.CanCollide = true
end
wait(6)
Button1.BrickColor = BrickColor.Green()
for _, Platform in pairs(Platforms:GetChildren() do -- loop through every platform
Platform.Transparency = 1
Platform.CanCollide = false
end
end)
local PP = game.Workspace.Button1.ProximityPrompt
local Button1 = game.Workspace.Button1
local Baseplate = game.Workspace.Baseplate
local Platforms = workspace.Platforms
PP.Triggered:Connect(function()
Button1.BrickColor = BrickColor.Red()
for _, Platform in pairs(Platforms:GetChildren()) do -- loop through every platform
Platform.Transparency = 0
Platform.CanCollide = true
end
wait(6)
Button1.BrickColor = BrickColor.Green()
for _, Platform in pairs(Platforms:GetChildren()) do -- loop through every platform
Platform.Transparency = 1
Platform.CanCollide = false
end
end)
local PP = game.Workspace.Button1.ProximityPrompt
local Button1 = game.Workspace.Button1
local Baseplate = game.Workspace.Baseplate
local Platforms = workspace.Platforms
PP.Triggered:Connect(function()
Button1.BrickColor = BrickColor.Red()
for _, Platform in pairs(Platforms:GetChildren()) do
Platform.Transparency = 0
Platform.CanCollide = true
end
wait(6)
Button1.BrickColor = BrickColor.Green()
for _, Platform in pairs(Platforms:GetChildren()) do -- loop through every platform
Platform.Transparency = 1
Platform.CanCollide = false
end
end)
local Button1 = workspace.Button1
local ProximityPrompt = Button1.ProximityPrompt
local Baseplate = workspace.Baseplate
local Platforms = workspace.Platforms
local Debounce = true
function SetPlatforms(Color, Transparency, CanCollide)
Button1.BrickColor = BrickColor[Color]()
for _, Platform in ipairs(Platforms:GetChildren()) do
Platform.Transparency = Transparency
Platform.CanCollide = CanCollide
end
end
ProximityPrompt.Triggered:Connect(function()
if Debounce then
Debounce = false
SetPlatforms("Red", 0, true)
task.wait(6)
SetPlatforms("Green", 1, false)
Debounce = true
end
end)
this code should be a good improvement from the solution
This is incorrect. Wait global (alongside spawn and delay) is not deprecated and likely will not be for a while. The reason to not use it is because it will see deprecation in the future.