Script doesnt wait for a function to return

yeah the title pretty much explains my problem
my function returns nil before even finishing, the rest of the script works fine
image

module.rollDice = function(dice)
	local faces={
		["Back"]=5,
		["Bottom"]=6,
		["Front"]=2,
		["Left"]=4,
		["Right"]=3,
		["Top"]=1
	}

	local function getsurface(surfacenormal)
		local normal = dice.PrimaryPart.CFrame:VectorToObjectSpace(surfacenormal)
		local nx,ny,nz = math.round(normal.X),math.round(normal.Y),math.round(normal.Z)
		if nx ~= 0 and (ny == 0 and nz == 0) then
			if nx > 0 then
				return("Front")
			else
				return("Back")
			end
		elseif ny ~= 0 and (nx == 0 and nz == 0) then
			if ny > 0 then
				return("Top")
			else
				return("Bottom")
			end
		elseif nz ~= 0 and (nx == 0 and ny == 0) then
			if nz > 0 then
				return("Left")
			else
				return("Right")
			end
		end
	end
	local Tween1 = TS:Create(dice.PrimaryPart,TweenInfo.new(0.1,Enum.EasingStyle.Linear),{["CFrame"]=CFrame.new(dice.PrimaryPart.Position)*CFrame.Angles(math.rad(90*math.random(1,4)),math.rad(90*math.random(1,4)),math.rad(90*math.random(1,4)))})
	Tween1:Play()
	Tween1.Completed:Connect(function()
		local Tween2 =  TS:Create(dice.PrimaryPart,TweenInfo.new(0.1,Enum.EasingStyle.Linear),{["CFrame"]=CFrame.new(dice.PrimaryPart.Position)*CFrame.Angles(math.rad(90*math.random(1,4)),math.rad(90*math.random(1,4)),math.rad(90*math.random(1,4)))})
		Tween2:Play()
		Tween2.Completed:Connect(function()
			local Tween3 =  TS:Create(dice.PrimaryPart,TweenInfo.new(0.1,Enum.EasingStyle.Back),{["CFrame"]=CFrame.new(dice.PrimaryPart.Position)*CFrame.Angles(math.rad(90*math.random(1,4)),math.rad(90*math.random(1,4)),math.rad(90*math.random(1,4)))})
			Tween3:Play()
			Tween3.Completed:Connect(function()
				local params = RaycastParams.new()
				params.FilterType = Enum.RaycastFilterType.Whitelist
				params.FilterDescendantsInstances = {dice.PrimaryPart}
				local origin = dice.PrimaryPart.Position+Vector3.new(0,dice.PrimaryPart.Size.Y,0)
				local results = workspace:Raycast(origin,dice.PrimaryPart.Position-origin,params)
				local surface = getsurface(results.Normal)
				print(faces[surface]) -- line 52
				return faces[surface]
			end)
		end)
	end)
end
local num = module.rollDice(map.Dice) -- line 101
print(num)

You’re making an entirely separate function here:

Tween1.Completed:Connect(function()
--                           ^
--                          here

It’s going to return to this function and not rollDice. Instead of using Tween1.Completed:Connect() use Tween1.Completed:Wait(), then paste the code after it.

You can’t connect a roblox function off an instance event like that.

Tween3.Completed:Connect(function()

The code above returns a RobloxScriptConnection instance. What you need to do is pass the function with the parameters which are required.

works like a charm, thanks mate!

You can’t pass arguments to callback functions connected to events via the connection instance method “:Connect()”, they receive their arguments from their respective events instead.