Scripting Challenges

I don’t know if this is against the rules or not but, guys I need ideas for me to script. Or atleast challenges, got any ideas?

10 Likes

If you’re a little advanced at scripting, I recommend you check out this series (only the first part out as of now):

8 Likes

Thanks, Luckily I know a bit of French but only enough to go up to probably 20.

2 Likes

hackerrank.com is a fun place to kill some time with programming challenges, and it can also help with practicing for the kinds of quizzes you see in job interviews.

3 Likes

A few months ago I was bored and tried a simple chalenge you might enjoy as well.
Could you build a cube (6 bricks as faces) in Roblox with just a for loop? You can only insert a block per iteration. No if statements. Just set the parts’ Position (or CFrame) and Size.

for i = 1, 6 do
...
end

gl, hf!

2 Likes

Can I initialize variables outside the for-loop?

Of course, but only for the sake of efficiency/readability. For example, declaring local part = Instance.new("Part"), or local side = 3 --studs.

What I mean is that Vector3 (or CFrame) values cannot be previously defined:
local pos = {Vector3.new(0, 0, 5), Vector3.new(0, 0, -5), etc.} -- cheater!! >:o

2 Likes

I’ll assume ternary operations aren’t allowed either, since they’re similar to if statements?
Like: local xAngle = (i > 4) and 0 or (i * math.pi / 2)

Hmm… I didn’t think about it… but why not? It was intended mainly for math operators and that stuff, but solving it as you suggested would be a bit challenging too. :thinking:


@AskWisp I guess you couldn’t think of anything harder… especially for the 6th challenge! :open_mouth:

1 Like

Here’s my cube script.

-- input declarations
local width = 4
local baseCF = CFrame.new(0, 10, 0)
local size = Vector3.new(width, 0.1, width)
local ninety = math.pi / 2

-- inner loop variables
local x, z, part

-- position them with X and Z coords

-- front: x = 90, z = 0		i = 1
-- top: x = 180, z = 0		i = 2
-- back: x = 270, z = 0		i = 3
-- bottom: x = 360, z = 0		i = 4
-- left: x = 0, z = 90		i = 5
-- right: x = 0, z = -90	i = 6


for i = 1, 6 do
	
	x = (i > 4) and 0 or (i * ninety)
	z = (i < 5) and 0 or (((-1) ^ i) * ninety)
	
	part = Instance.new("Part")
	part.Name = i
	part.Anchored = true
	part.Size = size
	part.CFrame = baseCF * CFrame.Angles(x, 0, z) * CFrame.new(0, width / -2, 0)
	part.Parent = workspace
	
end
1 Like

Cube.

code
local w = 5
local o = CFrame.new()
local s = Vector3.new(w, 0.05, w)

for i = 1, 6 do
	local x = (math.min(i-1, 4)%4)*math.pi/2
	local z = (2^(math.max(i, 4)-4)-1)*math.pi/2
	local p = Instance.new("Part")
	p.Anchored = true
	p.CFrame = o * CFrame.Angles(x, 0, z) * CFrame.new(0, w/2-s.Y/2, 0)
	p.Name = i
	p.Size = s
	p.Parent = workspace
end

what have I done

4 Likes

Beautiful!

My solution (it can hurt your eyes though):
local WorldPosition = Vector3.new()
local Side = 10

local Sample = Instance.new("Part")
Sample.Anchored = true

for i = 1, 6 do
	local Part = Sample:Clone()
	Part.Size = Vector3.new(Side * math.abs(1 - math.floor(1.5 - 0.2 * i)), Side * math.abs(1 - math.floor(0.2 * i)), Side * math.abs(1 - (1 - math.abs(math.floor(0.5 * i - 1.4)))))
	Part.Position = WorldPosition + Vector3.new((-1)^i * Side/2 * math.floor(1.5 - 0.2 * i), (-1)^i * Side/2 * math.floor(0.2 * i), (-1)^i * Side/2 * (1 - math.abs(math.floor(0.5 * i - 1.4))))
	Part.Parent = workspace
end

(This is totally not generalizable and totally not elegant.)

1 Like

Cube, again. Without using math.

code²
local w = 5
local o = CFrame.new()
local s = Vector3.new(w, 0.05, w)
local pihalfs = 1.5707963267948966

for i = 1, 6 do
	local x = (((i-1)/4)%1)*4*pihalfs
	local z = 2^(i-5)-(2^(i-5))%1
	z = z-z%1
	z = (z+(z/2)-(z/2)%1)*pihalfs
	local p = Instance.new("Part")
	p.Anchored = true
	p.CFrame = o * CFrame.Angles(x, 0, z) * CFrame.new(0, w/2-s.Y/2, 0)
	p.Name = i
	p.Size = s
	p.Parent = workspace
end

why is this even possible

5 Likes

How do you even come up with this? God, my knowledge of mathematics is just… nothing… :sweat_smile:

2 Likes

Here’s my code :

cubePosition = Vector3.yAxis * 10
cubeSize = Vector3.one * 10

for i = 1, 6 do
	local vectorTable = {1, 0, 0, 0}

	for _ = 1, i - 1 do
		local vectorTable2 = {0, 0, 0}

		for i2 = 1, 3 do
			vectorTable2[i2] = math.max(vectorTable2[i2] * 2 - 1, -vectorTable[i2])
			
			vectorTable2[i2 + 1] = math.max(0, -vectorTable[i2])
		end

		vectorTable = vectorTable2
	end

	for i2 = 1, 3 do
		vectorTable[i2] = math.abs(vectorTable[i2] - (vectorTable[i2 + 1])) * vectorTable[i2]
	end
	
	local sizeVectorTable = {}
	
	for i2 = 1, 3 do
		sizeVectorTable[i2] = 1 - math.abs(vectorTable[i2])
	end
	
	local part = Instance.new("Part")
	part.Anchored = true
	part.Position = cubePosition + Vector3.new(unpack(vectorTable)) * cubeSize / 2
	part.Size = cubeSize * Vector3.new(unpack(sizeVectorTable))
	part.Parent = workspace
end
1 Like