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

Help Me On This Block Extractor???

I Want Rock To Pass But The Coal To Get Teleported Some Place Else I Made The Script But, It Won't Work Any Help?

positions = {[1] = -32.012, 2.03, -48.3,
			 [2] = -33.302, 2.03, -48.3,
			 [3] = -34.352, 2.03, -48.3,
			 [4] = -35.412, 2.03, -48.3,
			 [5] = -36.472, 2.03, -48.3}

function onTouched(hit)
	if hit.Name == "Coal" then
	hit.Position = (positions[math.random(1, #positions)])
	elseif hit.Name == "CleanedCoal" then
	hit.Position = (positions[math.random(1, #positions)])
	elseif hit.Name == "CleanedPolishedCoal" then
	hit.Position = (positions[math.random(1, #positions)])
	end
end

script.Parent.Touched:connect(onTouched)

1 answer

✓ Accepted answer

Tables on ROBLOX are weird. The way to have another array/table inside of a table is to do this:

positions = {{-32.012, 2.03, -48.3},
             {-33.302, 2.03, -48.3},
             {-34.352, 2.03, -48.3},
             {-35.412, 2.03, -48.3},
             {-36.472, 2.03, -48.3}}

function onTouched(hit)
    pos = positions[math.random(1,#positions)] -- Even if it isn't the right block, just makes a value so you don't have to do it for each if-then pair
    if hit.Name == "Coal" then
    hit.Position = Vector3.new(pos[1],pos[2],pos[3]) -- Access the different positions inside the extracted table
    elseif hit.Name == "CleanedCoal" then
    hit.Position = Vector3.new(pos[1],pos[2],pos[3])
    elseif hit.Name == "CleanedPolishedCoal" then
    hit.Position = Vector3.new(pos[1],pos[2],pos[3])
    end
end

script.Parent.Touched:connect(onTouched)

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