✓ Accepted answer
You just need to use a Local Variable.
To make a variable local, simply put the keyword local in front of any variable.
function onTouched()
local dropplankchoice = math.random(1,6)
if dropplankchoice == 1 then
game.Workspace.dropplank1:Destroy()
game.Workspace.dropplank2:Destroy()
end
if dropplankchoice == 2 then
game.Workspace.dropplank3:Destroy()
game.Workspace.dropplank4:Destroy()
end
if dropplankchoice == 3 then
game.Workspace.dropplank1:Destroy()
game.Workspace.dropplank3:Destroy()
end
if dropplankchoice == 4 then
game.Workspace.dropplank2:Destroy()
game.Workspace.dropplank4:Destroy()
end
if dropplankchoice == 5 then
game.Workspace.dropplank1:Destroy()
game.Workspace.dropplank4:Destroy()
end
if dropplankchoice == 6 then
game.Workspace.dropplank2:Destroy()
game.Workspace.dropplank3:Destroy()
end
end
script.Parent.Touched:connect (onTouched)
Simplifying your script
There are many things that can be done to simplify you script. Let's run down the list.
Use elseif
You don't need the extra ends this way.
function onTouched()
local dropplankchoice = math.random(1,6)
if dropplankchoice == 1 then
game.Workspace.dropplank1:Destroy()
game.Workspace.dropplank2:Destroy()
elseif dropplankchoice == 2 then
game.Workspace.dropplank3:Destroy()
game.Workspace.dropplank4:Destroy()
elseif dropplankchoice == 3 then
game.Workspace.dropplank1:Destroy()
game.Workspace.dropplank3:Destroy()
elseif dropplankchoice == 4 then
game.Workspace.dropplank2:Destroy()
game.Workspace.dropplank4:Destroy()
elseif dropplankchoice == 5 then
game.Workspace.dropplank1:Destroy()
game.Workspace.dropplank4:Destroy()
elseif dropplankchoice == 6 then
game.Workspace.dropplank2:Destroy()
game.Workspace.dropplank3:Destroy()
end
end
script.Parent.Touched:connect (onTouched)
Don't check for the variable
Instead, you could loop for dropplank(1,2,3,4) and destroy it.
function onTouched()
local dropplank = workspace:FindFirstChild("dropplank"..math.random(4))
dropplank:Destroy()
end
script.Parent.Touched:connect (onTouched)
This might not work.
You should also add a debounce.
local db = false
function onTouched(d)
if db then return end
db = true
local dropplank = workspace:FindFirstChild("dropplank"..math.random(4))
dropplank:Destroy()
wait(2)
db = false
end
script.Parent.Touched:connect (onTouched)
Probably wont work
Good Luck!
If I helped, please don't forget to accept my answer