Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how do i use an onTouch and math.random in this case?

function onTouched()
	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)

So i made this script and put it inside a part, and when that part is touched, it does a math.random and triggers an event. Only problem is its not working an dropplankchoice at the top is underlined in blue.

dropplankchoice = math.random(1,6)

That's the one underlined in blue. And i can't seem to fix it, can anyone help me and explain to me what I'm doing wrong?

1 answer

✓ 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

This discussion is preserved as it was. New answers, comments, and votes are closed.