I am making a button that when you touch it makes blocks appear. And after a certain time they vanish. But it seems the on touch doesn’t do anything. When I stand on the button it does nothing. It was designed to push down and turn red, but since the onTouch doesn’t do anything it never makes it this far.
Script used:
script.Parent.BrickColor = BrickColor.new("Dark green")
startPosition = Vector3.new(-1.1, 0.9, 10.1)
local a = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Sphere1
a.CanCollide = false
a.Transparency = 1
local b = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Wedge1
b.CanCollide = false
b.Transparency = 1
local c = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Wedge2
c.CanCollide = false
c.Transparency = 1
local d = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Cylinder1
d.CanCollide = false
d.Transparency = 1
local Button = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Button"].Button1.One
local buttonactive = false
function onTouch()
if buttonactive == false then
buttonactive = true
displaytime(15)
else print ("Error - Already active!")
end
end
function displaytime(countdowntime)
local timer = Instance.new("BillboardGui", game.Workspace.Button1.One)
timer.Size = UDim2.new(0,50,0,50)
timer.StudsOffset = Vector3.new(0,5,0)
timer.AlwaysOnTop = true
print("first displaytime sucess")
local timertxt = Instance.new("TextBox", game.Workspace.Button1.One)
timertxt.Parent = timer
timertxt.Size = UDim2.new(0,50,0,50)
timertxt.TextScaled = true
timertxt.TextColor3 = Color3.new(255,255,255)
timertxt.BackgroundColor3 = Color3.new(0,0,0)
timertxt.BorderColor3 = Color3.new(0,0,0)
timertxt.BackgroundTransparency = 1
local bincountdown = countdowntime
timer.Enabled = true
a.CanCollide = true
a.Transparency = 0
b.CanCollide = true
b.Transparency = 0
c.CanCollide = true
c.Transparency = 0
d.CanCollide = true
d.Transparency = 0
local function ButtonAnimation()
local obstruction = Button
obstruction.Position = Vector3.new(-1.1,0.8,10.1)
wait (0.005)
local obstruction2 = Button
obstruction.Position = Vector3.new(-1.1,0.7,10.1)
wait (0.005)
local obstruction3 = Button
obstruction.Position = Vector3.new(-1.1,0.6,10.1)
wait (0.005)
local obstruction4 = Button
obstruction.Position = Vector3.new(-1.1,0.5,10.1)
script.Parent.BrickColor = BrickColor.new("Really red")
repeat
timertxt.Text = tostring(bincountdown)
bincountdown = bincountdown - 1
wait(1)
until
bincountdown < 1
timer.Enabled = false
buttonactive = false
b.CanCollide = false
b.Transparency = 1
script.Parent.BrickColor = BrickColor.new("Dark green")
end
script.Parent.Touched:connect(onTouch)
wait(0.1)
end
So when I remove
a.CanCollide = false
a.Transparency = 1
local b = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Wedge1
b.CanCollide = false
b.Transparency = 1
local c = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Wedge2
c.CanCollide = false
c.Transparency = 1
local d = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Cylinder1
d.CanCollide = false
d.Transparency = 1
local Button = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Button"].Button1.One
The onTouch does work and the button does as I wanted, but with out the variables which I need to make the blocks appear. Did I do something wrong when I added variables?
Also I don’t know much about scripting so this script may not be efficient
You never actually call the ButtonAnimation() function within the onTouch() function, so it never gets executed.
The onTouch() function is global, so what the script is doing actually does work. However global functions/variables shouldn’t be used in Luau (Roblox’s version of Lua).
Here’s a somewhat fixed version of your script, with the functions moved around and stuff cleaned up. I reccomend reading some of the comments that highlight what’s been done
script.Parent.BrickColor = BrickColor.new("Dark green")
-- variables and functions should always be local
local startPosition = Vector3.new(-1.1, 0.9, 10.1)
local a = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Sphere1
a.CanCollide = false
a.Transparency = 1
local b = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Wedge1
b.CanCollide = false
b.Transparency = 1
local c = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Wedge2
c.CanCollide = false
c.Transparency = 1
local d = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Blocks"].Cylinder1
d.CanCollide = false
d.Transparency = 1
local Button = game.Workspace["Obby Structure"]["Levels 1-10"]["1"]["Button"].Button1.One
local buttonactive = false
local function displaytime(countdowntime)
local timer = Instance.new("BillboardGui", game.Workspace.Button1.One)
timer.Size = UDim2.new(0,50,0,50)
timer.StudsOffset = Vector3.new(0,5,0)
timer.AlwaysOnTop = true
print("first displaytime sucess")
local timertxt = Instance.new("TextBox", game.Workspace.Button1.One)
timertxt.Parent = timer
timertxt.Size = UDim2.new(0,50,0,50)
timertxt.TextScaled = true
timertxt.TextColor3 = Color3.new(255,255,255)
timertxt.BackgroundColor3 = Color3.new(0,0,0)
timertxt.BorderColor3 = Color3.new(0,0,0)
timertxt.BackgroundTransparency = 1
timer.Enabled = true
a.CanCollide = true
a.Transparency = 0
b.CanCollide = true
b.Transparency = 0
c.CanCollide = true
c.Transparency = 0
d.CanCollide = true
d.Transparency = 0
-- i've done away with the ButtonAnimation function completely, since it's only used once
local obstruction = Button
obstruction.Position = Vector3.new(-1.1,0.8,10.1)
wait(0.005)
local obstruction2 = Button
obstruction.Position = Vector3.new(-1.1,0.7,10.1)
wait(0.005)
local obstruction3 = Button
obstruction.Position = Vector3.new(-1.1,0.6,10.1)
wait(0.005)
local obstruction4 = Button
obstruction.Position = Vector3.new(-1.1,0.5,10.1)
script.Parent.BrickColor = BrickColor.new("Really red")
local bincountdown = countdowntime
repeat
timertxt.Text = tostring(bincountdown)
bincountdown -= 1 -- compund operator
wait(1)
until bincountdown < 1
timer.Enabled = false
buttonactive = false
b.CanCollide = false
b.Transparency = 1
script.Parent.BrickColor = BrickColor.new("Dark green")
end
local function onTouch()
if buttonactive == false then
buttonactive = true
displaytime(15)
else
print ("Error - Already active!")
end
end
script.Parent.Touched:Connect(onTouch) -- capital C on ":connect"
altho the touch will be always active if a part touches it, try doing the touch like that
local function onTouch(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if buttonactive == false then
buttonactive = true
displaytime(15)
else
print ("Error - Already active!")
end
end
end